Maestro13
Maestro13

Reputation: 3696

How add empty namespace to simpleXML element

In order to simulate XML retrieved from a SOAP web service in a local php test environment version of the web service, I wish to add an empty namespace to an element, just as supplied by the actual web service itself.

But when I add a null string namespace argument to the addChild method, it is not added. Any other value does give me a namespace, but as I said I wish to have an empty one.

So

$xml = new SimpleXMLElement('<cdhead/>');
$nako = $xml -> addChild ('nako', '000', '');

gives me

<cdhead>
    <nako>000</nako>
</cdhead>

But I wish to have

<cdhead>
    <nako xmlns="">000</nako>
</cdhead>

How can I achieve that?

Upvotes: 0

Views: 395

Answers (1)

hek2mgl
hek2mgl

Reputation: 158030

This problem is because you are using php 5.2. Its a bug and was fixed in the PHP 5.2 series. Here you can view the Bugreport. I've tested using PHP 5.3 and your example works out of the box. This should work in any case:

<?php

$xml = new SimpleXMLElement('<cdhead/>');
$nako = $xml -> addChild ('nako', '000');
$nako->addAttribute('xmlns', '');

echo $nako->saveXml();

It adds the attribute manually if the PHP version is lower than 5.3

UPDATE I tried this with PHP 5.2.17-dotdeb too and it worked out of the box, both

  • addAttribute
  • addChild with empty string as namespace

Upvotes: 1

Related Questions