如何防止php自动关闭,如何防止php simplexml中的自动关闭标签

根据

spec,LIBXML_NOEMPTYTAG不适用于simplexml:

This option is currently just available in the DOMDocument::save and DOMDocument::saveXML functions.

为了实现你以后的目标,你需要将simplexml对象转换成一个DOMDocument对象:

$xml = new SimpleXMLElement('');

$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);

它返回:

value

值得指出的是,NOEMPTYTAG选项可用于DOMDocument而不是simplexml的可能原因是,空元素不被视为有效的XML,而DOM规范允许它们.你正在将你的头撞到墙上,以获得无效的XML,这可能表明有效的自我关闭的空元素也可以工作.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值