使用 SimpleXML 创建 XML 时省略命名空间前缀


Omit namespace prefix when creating XML using SimpleXML

>我有以下代码

$base = '<?xml version="1.0" encoding="UTF-8"?><realestates:office xmlns:realestates="http://rest.immobilienscout24.de/schema/offer/realestates/1.0" xmlns:xlink="http://www.w3.org/1999/xlink"></realestates:office>';
$objXml = new 'SimpleXMLElement($base);
$objXml->addChild('title', 'Alles Toller');
$strXml = $objXml->asXML();

$strXml现在将是

<?xml version="1.0" encoding="UTF-8"?>
<realestates:office xmlns:realestates="http://rest.immobilienscout24.de/schema/offer/realestates/1.0" xmlns:xlink="http://www.w3.org/1999/xlink">    
<realestates:title>Alles Toller</realestates:title>
</realestates:office>

我想要的是<title>中没有realestates:前缀

<?xml version="1.0" encoding="UTF-8"?>
<realestates:office xmlns:realestates="http://rest.immobilienscout24.de/schema/offer/realestates/1.0" xmlns:xlink="http://www.w3.org/1999/xlink">    
<title>Alles Toller</title>
</realestates:office>

我怎样才能做到这一点?

我最终使用了DOMDocument

 $objXml = new 'DOMDocument( "1.0", "UTF-8" );
 $objRoot = $objXml->createElement('realestates:office');
 $objXml->appendChild($objRoot);
 $objXml->createAttributeNS('http://rest.immobilienscout24.de/schema/offer/realestates/1.0', 'realestates:office');
 $objXml->createAttributeNS('http://www.w3.org/1999/xlink', 'xlink:dummy');
 $objTitle = $objXml->createElement('title', 'Alles Toll');
 $objRoot->appendChild($objTitle);
 $strXml = $objXml->saveXML();

createAttributeNS()的第二个参数有点奇怪——只有命名空间是不够的,看起来我还必须添加一个元素名称。