开发者

PHP format xml output

开发者 https://www.devze.com 2023-03-28 09:59 出处:网络
This php script doesn\'t ouput the formatted XML that I expect from specifying $dom->formatOutput = true;

This php script doesn't ouput the formatted XML that I expect from specifying

$dom->formatOutput = true; 
$dom->preserveWhiteSpace = false;

PHP Script

<?php

$dom = new DOMDocument();
$dom->load('communities.xml');

$dom->formatOutput = true; 
$dom->preserveWhiteSpace = false;

// get document element  

$root   = $dom->documentElement;  

$name     = $dom->createElement("NAME");  
$nameText = $dom->createTextNode("Hobbies");  
$name->appendChild($nameText); 

$community = $dom->createElement('COMMUNITY');

$community->append开发者_如何学编程Child($name);  
$root->appendChild($community); 

$dom->save('communities.xml');

$tidy_options = array(
                'input-xml'    => true,
                'output-xml'   => true,
                'indent'       => true,
                'wrap'         => false,
        );
$tidy = new tidy();
$tidy->parseFile('communities.xml', $tidy_options);
$tidy->cleanRepair();
echo $tidy;

?>

Is anything else needed to get a formatted output with each tag indented and on one line?

0

精彩评论

暂无评论...
验证码 换一张
取 消