2017-08-07 66 views
2

我有SimpleXML中自闭标签的问题。例如,我的XML文件:SimpleXML:不扩展自闭标签

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<root> 
<a>hello</a> 
<b attr="1"/> 
</root> 

PHP代码:

$xml = simplexml_load_file($path); 
echo $xml->asXML(); 

输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<root> 
<a>hello</a> 
<b attr="1"></b> 
</root> 

正如你所看到的,SimpleXML的转换自闭标签<b attr="1"/><b attr="1"></b>。我不需要这个。如何防止这种转换?

回答

2

更改您加载XML到

$xml = simplexml_load_file($path, null, LIBXML_NOEMPTYTAG); 

这给了路......

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<root> 
<a>hello</a> 
<b attr="1"/> 
</root>