格式xml字符串 [英] format xml string
本文介绍了格式xml字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PHP类中格式化XML的最佳方法是什么.
What is the best way to format XML within a PHP class.
$xml = "<element attribute=\"something\">...</element>";
$xml = '<element attribute="something">...</element>';
$xml = '<element attribute=\'something\'>...</element>';
$xml = <<<EOF
<element attribute="something">
</element>
EOF;
我很确定这是最后一个!
I'm pretty sure it is the last one!
推荐答案
使用 DOM 您可以
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML('<root><foo><bar>baz</bar></foo></root>');
$dom->formatOutput = TRUE;
echo $dom->saveXML();
提供(实时演示)
<?xml version="1.0"?>
<root>
<foo>
<bar>baz</bar>
</foo>
</root>
请参见 DOMDocument::formatOutput
和 DOMDocument::preserveWhiteSpace
属性描述.
See DOMDocument::formatOutput
and DOMDocument::preserveWhiteSpace
properties description.
这篇关于格式xml字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文