我有一个 simplexml 脚本,用于发布从表单输入的数据。
$xml = simplexml_load_file("links.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$person = $sxe->addChild("link");
$person->addChild("title", "Q: ".$x_title);
$person->addChild("url", "questions_layout.php#Question$id");
$sxe->asXML("links.xml");
当它出来时,它看起来像这样一行:
<link><title>Alabama State</title><url>questions_layout.php#Question37</url></link><link><title>Michigan State</title><url>questions_layout.php#Question37</url></link></pages>
但我已经尝试过找到的方法HERE https://stackoverflow.com/questions/1840148/php-simplexml-new-line and 这也是 https://stackoverflow.com/questions/798967/php-simplexml-how-to-save-the-file-in-a-formatted-way但两者都没有正确地格式化行中的 XML,因为它们应该是这样的
<link>
<title></title>
<url></url>
</link>
在第一个参考链接中我什至改变了loadXML
to load
, 因为loadXML
需要 XML 形式的字符串。有人可以帮我找到解决方案吗?
AFAIK simpleXML 无法独自做到这一点。
然而,DOMDocument 可以。
$dom = dom_import_simplexml($sxe)->ownerDocument;
$dom->formatOutput = TRUE;
$formatted = $dom->saveXML();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)