【问题标题】:PHP XML how to output nice formatPHP XML如何输出漂亮的格式
【发布时间】:2012-01-26 18:37:40
【问题描述】:

代码如下:

$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.

我刚刚得到

<xml><child>ee</child></xml>

我想成为 utf-8 这怎么可能?

【问题讨论】:

  • 关于您的 utf-8 问题,只需将其作为第二个参数添加到对象中,例如 $doc = new DOMDocument("1.0", "UTF-8");

标签: php xml domdocument


【解决方案1】:

您可以尝试这样做:

...
// 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;

这可能更简洁。两种情况下的输出都是 (Demo):

<?xml version="1.0"?>
<root>
  <error>
    <a>eee</a>
    <b>sd</b>
    <c>df</c>
  </error>
  <error>
    <a>eee</a>
    <b>sd</b>
    <c>df</c>
  </error>
  <error>
    <a>eee</a>
    <b>sd</b>
    <c>df</c>
  </error>
</root>

我不知道如何使用DOMDocument 更改缩进字符。您可以使用基于逐行正则表达式的替换(例如使用preg_replace)对 XML 进行后处理:

$xml_string = preg_replace('/(?:^|\G)  /um', "\t", $xml_string);

另外,tidy extension with tidy_repair_string 也可以漂亮地打印 XML 数据。可以用它指定缩进级别,但是 tidy 永远不会输出制表符。

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

【讨论】:

【解决方案2】:

使用 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指定的新文件

【讨论】:

  • @quickshiftin - 输入数据是SimpleXMLElement 的一个实例。我将编辑答案以使其更加明显。不管怎样,我同意你给DOMDocument 喂的东西实际上是无关紧要的。
  • 此外,您可以在loadXML 之后在save 之前使用`$domxml->encoding = "UTF-8"`。
【解决方案3】:
<?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);

【讨论】:

  • 你能不能再补充一个解释?
【解决方案4】:

尝试了所有答案,但都没有奏效。也许是因为我在保存 XML 之前添加和删除了子项。 经过大量谷歌搜索后,在 php 文档中找到了this comment。我只需要重新加载生成的 XML 即可使其工作。

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

【讨论】:

    【解决方案5】:
    // ##### 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
    

    【讨论】:

    • 赞成,因为这个答案包含一个完整的例子!
    • 很好的答案,这是在我的情况下完全有效的唯一选择(使用 RSS XML)。
    【解决方案6】:

    这里有两个不同的问题:

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

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


    另请参阅 xmlEncodingencoding

    【讨论】:

    • preserveWhiteSpace = TRUE 可能会妨碍您使用DOMDocument 进行漂亮的打印 - 仅供参考,而不是问题给出的示例,但如果您从实际具有空白文本节点的现有文件加载。
    • @hakre 为什么preserveWhiteSpace = TRUE 可以很好地处理 XML,但不能处理 HTML?
    • 要让浏览器格式化,必须设置正确的 MIME 类型。例如 whit: header('Content-type: text/xml');
    • 我实际上需要看到空格,所以这对我来说是正确的答案
    【解决方案7】:

    这是上述主题的轻微变化,但我将其放在这里以防其他人遇到此问题并且无法理解它......就像我所做的那样。

    当使用 saveXML() 时,目标 DOM 文档中的 preserveWhiteSpace 不适用于导入的节点(在 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-&gt;saveXML(); 语句不会漂亮地打印从 $message 导入的内容,但最初在 $dom 中的内容会被漂亮地打印出来。

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

    $message->preserveWhiteSpace = false; 
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 2011-05-06
      • 2020-01-24
      • 2017-08-24
      • 2013-11-19
      • 1970-01-01
      相关资源
      最近更新 更多