php $SimpleXML->asXML() 的格式输出;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1191167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Format output of $SimpleXML->asXML();
提问by Eli
The title pretty much says it all.
标题基本概括了所有内容。
If I have something like (from PHP site examples):
如果我有类似的东西(来自 PHP 站点示例):
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies></movies>
XML;
$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');
$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');
$characters = $movie->addChild('characters');
$character = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');
$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');
echo("<pre>".htmlspecialchars($sxe->asXML())."</pre>");
die();
I end up outputing a long string like so:
我最终输出了一个长字符串,如下所示:
<?xml version="1.0" standalone="yes"?>
<movies type="documentary"><movie><title>PHP2: More Parser Stories</title><plot>This is all about the people who make it work.</plot><characters><character><name>Mr. Parser</name><actor>John Doe</actor></character></characters><rating type="stars">5</rating></movie></movies>
This is fine for a program consumer, but for debugging/human tasks, does anyone know how to get this into a nice indented format?
这对程序使用者来说很好,但是对于调试/人工任务,有谁知道如何将其转换为漂亮的缩进格式?
回答by Adam Wright
There's a variety of solutions in the comments on the PHP manual page for SimpleXMLElement. Not very efficient, but certainly terse, is a solution by Anonymous
在SimpleXMLElement的PHP 手册页的注释中有多种解决方案。效率不高,但确实简洁,是 Anonymous 的解决方案
$dom = dom_import_simplexml($simpleXml)->ownerDocument;
$dom->formatOutput = true;
echo $dom->saveXML();
The PHP manual page comments are often good sources for common needs, as long as you filter out the patently wrong stuff first.
PHP 手册页注释通常是满足常见需求的良好来源,只要您首先过滤掉明显错误的内容。
回答by Bidders
The above didn't work for me, I found this worked:
以上对我不起作用,我发现这有效:
$dom = new DOMDocument("1.0");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($simpleXml->asXML());
echo $dom->saveXML();
回答by naxrohan
Found a similar solution...to format raw xlm data..from my php SOAPrequests __getLastRequest & __getLastResponse, for quick debugging the xml's i have combined it with google-code-prettify.
找到了一个类似的解决方案...格式化原始 xlm 数据...来自我的php SOAP请求__getLastRequest & __getLastResponse,为了快速调试 xml,我将它与google-code-prettify.
Its a good solution if you want to format sensitive xml data and don't want to do it online.
如果您想格式化敏感的 xml 数据并且不想在线进行,它是一个很好的解决方案。
Some sample code below, may be helpful to others:
下面的一些示例代码,可能对其他人有帮助:
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($data); //=====$data has the raw xml data...you want to format
echo '<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>';
echo "<br/> <pre class=\"prettyprint\" >". htmlentities($dom->saveXML())."</pre>";
Below is a sample of the Formatted XML Output I got:
下面是我得到的格式化 XML 输出的示例:
Note:The formatted XML is available in $dom->saveXML()and can be directly saved to a xml file using php file write.
注意:格式化后的 XML 是可用的$dom->saveXML(),可以使用 php file write 直接保存到 xml 文件中。


