脚本宝典收集整理的这篇文章主要介绍了如何防止php simplexml中的自动关闭标签,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
$xml = new SimpleXMLElement('<xml/>'); $output = $xml->addChild('child1'); $output->addChild('child2',"value"); $output->addChild('noValue',''); Header('Content-tyPE: text/xml'); PRint($xml->asXML());
输出是
<xml> <child1> <child2>value</child2> <noValue/> </child1> </xml>
<noValue></noValue>
我试过从Turn OFF self-closing tags in SimpleXML for PHP?使用LIBXML_NOEMPTYTAG
我试过$xml = new SimpleXMLElement(‘< xml />‘,LIBXML_NOEMPTYTAG);它不行.所以我不知道把LIBXML_NOEMPTYTAG放在哪里
为了实现你以后的目标,你需要将simplexml对象转换成一个DOMDocument对象:
$xml = new SimpleXMLElement('<xml/>'); $child1 = $xml->addChild('child1'); $child1->addChild('child2',"value"); $child1->addChild('noValue',''); $dom_sxe = dom_import_simplexml($xml); // Returns a DomElement object $dom_output = new DOMDocument('1.0'); $dom_output->formatOutput = true; $dom_sxe = $dom_output->importNode($dom_sxe,true); $dom_sxe = $dom_output->appendChild($dom_sxe); echo $dom_output->saveXML($dom_output,LIBXML_NOEMPTYTAG);
它返回:
<?xml version="1.0" encoding="UTF-8"?> <xml> <child1> <child2>value</child2> <noValue></noValue> </child1> </xml>
值得指出的是,NOEMPTYTAG选项可用于DOMDocument而不是simplexml的可能原因是,空元素不被视为有效的XML,而DOM规范允许它们.你正在将你的头撞到墙上,以获得无效的XML,这可能表明有效的自我关闭的空元素也可以工作.
以上是脚本宝典为你收集整理的如何防止php simplexml中的自动关闭标签全部内容,希望文章能够帮你解决如何防止php simplexml中的自动关闭标签所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。