PHP XML如何输出nice格式

PHP XML如何输出nice格式,php,xml,domdocument,Php,Xml,Domdocument,代码如下: $doc = new DomDocument('1.0'); // create root node $root = $doc->createElement('root'); $root = $doc->appendChild($root); $signed_values = array('a' => 'eee', 'b' => 'sd', 'c' => 'df'); // process one row at a time foreach ($sign

代码如下:

$doc = new DomDocument('1.0');
// create root node
$root = $doc->createElement('root');
$root = $doc->appendChild($root);
$signed_values = array('a' => 'eee', 'b' => 'sd', 'c' => 'df');
// process one row at a time
foreach ($signed_values as $key => $val) {
    // add node for each row
    $occ = $doc->createElement('error');
    $occ = $root->appendChild($occ);
    // add a child node for each field
    foreach ($signed_values as $fieldname => $fieldvalue) {
        $child = $doc->createElement($fieldname);
        $child = $occ->appendChild($child);
        $value = $doc->createTextNode($fieldvalue);
        $value = $child->appendChild($value);
    }
}
// get completed xml document
$xml_string = $doc->saveXML() ;
echo $xml_string;
如果我在浏览器中打印它,我不会得到像这样好的XML结构

<xml> \n tab <child> etc.
\n选项卡等。
我只是

<xml><child>ee</child></xml>
ee
我想成为utf-8
这一切怎么可能呢?

您可以尝试这样做:

...
// get completed xml document
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$xml_string = $doc->saveXML();
echo $xml_string;
您也可以在创建
DOMDocument
之后立即设置这些参数:

$doc = new DomDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
这可能更简洁。两种情况下的输出都是():

另外,还有一种可以很好地打印XML数据的方法。可以使用它指定缩进级别,但是tidy永远不会输出制表符

tidy_repair_string($xml_string, ['input-xml'=> 1, 'indent' => 1, 'wrap' => 0]);

这里有两个不同的问题:

  • 将和属性设置为
    TRUE
    ,以生成格式化的XML:

    $doc->formatOutput = TRUE;
    $doc->preserveWhiteSpace = TRUE;
    
  • 许多web浏览器(即internetexplorer和Firefox)在显示XML时对其进行格式化。使用“查看源”功能或常规文本编辑器检查输出



另请参见和。

对于SimpleXml对象,您可以

$domxml = new DOMDocument('1.0');
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
/* @var $xml SimpleXMLElement */
$domxml->loadXML($xml->asXML());
$domxml->save($newfile);
$xml
是您的simplexml对象


因此,您可以将simpleXml保存为一个由
$newfile

指定的新文件,这是上述主题的一个细微变化,但我在这里放了一个例子,以防其他人碰到这个问题,无法理解它…就像我做的那样

使用saveXML()时,目标DOMdocument中的保留空格不适用于导入的节点(在PHP 5.6中)

考虑以下代码:

$dom = new DOMDocument();                               //create a document
$dom->preserveWhiteSpace = false;                       //disable whitespace preservation
$dom->formatOutput = true;                              //pretty print output
$documentElement = $dom->createElement("Entry");        //create a node
$dom->appendChild ($documentElement);                   //append it 
$message = new DOMDocument();                           //create another document
$message->loadXML($messageXMLtext);                     //populate the new document from XML text
$node=$dom->importNode($message->documentElement,true); //import the new document content to a new node in the original document
$documentElement->appendChild($node);                   //append the new node to the document Element
$dom->saveXML($dom->documentElement);                   //print the original document
在此上下文中,
$dom->saveXML()语句不会很好地打印从$message导入的内容,但最初在$dom中的内容将很好地打印

为了实现整个$dom文档的漂亮打印,行:

$message->preserveWhiteSpace = false; 
必须包含在
$message=new DOMDocument()之后行-即,从中导入节点的文档也必须具有preserveWhiteSpace=false。

preserveWhiteSpace=false;
<?php

$xml = $argv[1];

$dom = new DOMDocument();

// Initial block (must before load xml string)
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
// End initial block

$dom->loadXML($xml);
$out = $dom->saveXML();

print_R($out);
$dom->formatOutput=true; //结束初始块 $dom->loadXML($xml); $out=$dom->saveXML(); 打印(输出);
尝试了所有答案,但没有一个有效。可能是因为我在保存XML之前附加和删除了child。 在php文档中进行了大量的谷歌搜索之后。我只需重新加载生成的XML即可使其正常工作

$outXML = $xml->saveXML(); 
$xml = new DOMDocument(); 
$xml->preserveWhiteSpace = false; 
$xml->formatOutput = true; 
$xml->loadXML($outXML); 
$outXML = $xml->saveXML(); 

preservewitspace=TRUE
在您使用
DOMDocument
进行漂亮的打印时可能会妨碍您的工作-仅供参考,不使用上述示例,但如果您从实际具有空白文本节点的现有文件加载。@hakre为什么
preservewitspace=TRUE
可以很好地处理XML,但不适用于HTML?要让浏览器格式化它,必须设置适当的MIME类型。例如whit:header('Content-type:text/xml');事实上,我需要查看空格,因此这是meRelated的正确答案:对于更受控的XML打印形式。相关:我发现“您也可以在创建DOMDocument之后设置这些参数”如果在处理/比较另一个文档时使用
saveXML
,这不是一个好主意,因为它可能会导致意外的结果。最好在需要输出之前格式化输出。@QuickShift-Input data是
SimpleXMLElement
的一个实例。我将编辑答案,使其更加明显。不管怎样,我同意你用
DOMDocument
输入的内容实际上是不相关的。此外,你可以在
加载xml
之后,在
保存之前,使用“$domxml->encoding=“UTF-8”。你还可以添加一个解释吗?关于你的UTF-8问题,只需将它作为第二个参数添加到对象中,如
$doc=new DOMDocument(“1.0”,“UTF-8”;
upvoted,因为这个答案包含了一个完整的示例!回答得很好,这是在我的环境下(使用RSS XML)完全有效的唯一选项。
<?php

$xml = $argv[1];

$dom = new DOMDocument();

// Initial block (must before load xml string)
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
// End initial block

$dom->loadXML($xml);
$out = $dom->saveXML();

print_R($out);
// ##### IN SUMMARY #####

$xmlFilepath = 'test.xml';
echoFormattedXML($xmlFilepath);

/*
 * echo xml in source format
 */
function echoFormattedXML($xmlFilepath) {
    header('Content-Type: text/xml'); // to show source, not execute the xml
    echo formatXML($xmlFilepath); // format the xml to make it readable
} // echoFormattedXML

/*
 * format xml so it can be easily read but will use more disk space
 */
function formatXML($xmlFilepath) {
    $loadxml = simplexml_load_file($xmlFilepath);

    $dom = new DOMDocument('1.0');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->loadXML($loadxml->asXML());
    $formatxml = new SimpleXMLElement($dom->saveXML());
    //$formatxml->saveXML("testF.xml"); // save as file

    return $formatxml->saveXML();
} // formatXML
$outXML = $xml->saveXML(); 
$xml = new DOMDocument(); 
$xml->preserveWhiteSpace = false; 
$xml->formatOutput = true; 
$xml->loadXML($outXML); 
$outXML = $xml->saveXML();