Cous
Cous

Reputation: 89

How to remove namespace with PHP SimpleXMLElement?

I have an XML structure that needs to look like this to work:

<yq1:ZwsCreateInfoRec xmlns:yq1="urn:test-com:document:test:soap:functions:mc-style">
      <ItPrice>1337</ItPrices>
</yq1:ZwsCreateInfoRec>

To try and achieve this I'm using SimpleXMLElement like this:

$xml = SimpleXMLElement('<yq1:ZwsCreteMaterialFrRef xmlns:yq1="urn:test-com:document:test:soap:functions:mc-style"></yq1:ZwsCreteMaterialFrRef>');
$xml->addChild('ItPrice', '1337');

When I now run $xml->asXML() it shows me this (Note the namespace on the ItPrice node):

<yq1:ZwsCreateInfoRec xmlns:yq1="urn:test-com:document:test:soap:functions:mc-style">
      <yq1:ItPrice>1337</yq1:ItPrices>
</yq1:ZwsCreateInfoRec>

For some reason the WebService doesn't work if I have the namespace on the child nodes like that but if I remove them it works. Is there a way for me to remove this or do I have to loop through this as a string and remove them manually?

Thanks

Upvotes: 0

Views: 298

Answers (1)

Jaydeep Chauhan
Jaydeep Chauhan

Reputation: 142

You can use 3rd parameter with blank value for addChild method. $xml->addChild('ItPrice', '1337', '');

$xml = new SimpleXMLElement('<yq1:ZwsCreteMaterialFrRef xmlns:yq1="urn:test-com:document:test:soap:functions:mc-style"></yq1:ZwsCreteMaterialFrRef>');
$namespaces = $xml->getDocNamespaces();
$xml->addChild('ItPrice', '1337', '');
echo  $xml->asXml();

Upvotes: 2

Related Questions