How can I display XML with whitespace formatting?

本小妞迷上赌 提交于 2019-12-02 04:07:34

I've found that formatOutput only works when preserveWhiteSpace is disabled:

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

Add this, it works for me.

echo '<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>';
echo "<br/> <pre class=\"prettyprint\" >". htmlentities($dom->saveXML($dom->firstChild->firstChild->firstChild))."</pre>"; 

You can delete $dom->firstChild->firstChild->firstChild for more info.

Try:

echo "<textarea cols='100' rows='40'>".htmlspecialchars($xml->asXML())."</textarea>";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!