PHP XML如何输出漂亮的格式

Dak*_*aka 61 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 ($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;
Run Code Online (Sandbox Code Playgroud)

如果我在浏览器中打印它,我就不会得到很好的XML结构

<xml> \n tab <child> etc.
Run Code Online (Sandbox Code Playgroud)

我得到了

<xml><child>ee</child></xml>
Run Code Online (Sandbox Code Playgroud)

我想成为utf-8这一切都有可能吗?

hak*_*kre 96

您可以尝试这样做:

...
// get completed xml document
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$xml_string = $doc->saveXML();
echo $xml_string;
Run Code Online (Sandbox Code Playgroud)

您可以在创建之后立即设置这些参数DOMDocument:

$doc = new DomDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
Run Code Online (Sandbox Code Playgroud)

这可能更简洁.两种情况下的输出都是(演示):

<?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>
Run Code Online (Sandbox Code Playgroud)

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

$xml_string = preg_replace('/(?:^|\G)  /um', "\t", $xml_string);
Run Code Online (Sandbox Code Playgroud)

另外,还有一个整洁的扩展tidy_repair_string,可以很好地打印XML数据.可以用它指定缩进级别,但是整洁不会输出制表符.

tidy_repair_string($xml_string, ['input-xml'=> 1, 'indent' => 1, 'wrap' => 0]);
Run Code Online (Sandbox Code Playgroud)


小智 30

使用SimpleXml对象,您可以简单地完成

$domxml = new DOMDocument('1.0');
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
/* @var $xml SimpleXMLElement */
$domxml->loadXML($xml->asXML());
$domxml->save($newfile);
Run Code Online (Sandbox Code Playgroud)

$xml 是你的simplexml对象

那么你就可以将simpleXml保存为指定的新文件 $newfile


小智 17

<?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);
Run Code Online (Sandbox Code Playgroud)

  • 你还可以添加解释吗? (2认同)

小智 6

// ##### 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
Run Code Online (Sandbox Code Playgroud)


小智 5

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

$outXML = $xml->saveXML(); 
$xml = new DOMDocument(); 
$xml->preserveWhiteSpace = false; 
$xml->formatOutput = true; 
$xml->loadXML($outXML); 
$outXML = $xml->saveXML(); 
Run Code Online (Sandbox Code Playgroud)