根据
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,这可能表明有效的自我关闭的空元素也可以工作.