1
<?xml version="1.0" encoding="ISO-8859-2"?>
    <!DOCTYPE pasaz:Envelope SYSTEM "loadOffers.dtd">
    <pasaz:Envelope xmlns:pasaz="http://schemas.xmlsoap.org/soap/envelope/">
        <pasaz:Body>
            <loadOffers xmlns="urn:ExportB2B">
                <offers />
            </loadOffers>
        </pasaz:Body>
    </pasaz:Envelope>

I've to add some child nodes to "offers" node and I'm using SimpleXML.

The PHP code: $offer = $xml->offers->addChild('offer') returns an error.

It's all wrong because I've got problem with handling namespaces in SimpleXML! Please help!

1 Answer 1

2

E.g. by using xpath the get the target/parent element.

<?php
$envelope = new SimpleXMLElement('<?xml version="1.0" encoding="ISO-8859-2"?>
<!DOCTYPE pasaz:Envelope SYSTEM "loadOffers.dtd">
<pasaz:Envelope xmlns:pasaz="http://schemas.xmlsoap.org/soap/envelope/">
  <pasaz:Body>
    <loadOffers xmlns="urn:ExportB2B">
      <offers />
    </loadOffers>
  </pasaz:Body>
</pasaz:Envelope>');

$envelope->registerXPathNamespace('pasaz', 'http://schemas.xmlsoap.org/soap/envelope/');
$envelope->registerXPathNamespace('b2b', 'urn:ExportB2B');
$ns = $envelope->xpath('//pasaz:Body/b2b:loadOffers/b2b:offers');
if ( 0<count($ns) ) {
  $offers = $ns[0];
  $offers->a = 'abc';
  $offers->x = 'xyz';
}
echo $envelope->asXml();

prints

<?xml version="1.0" encoding="ISO-8859-2"?>
<!DOCTYPE pasaz:Envelope SYSTEM "loadOffers.dtd">
<pasaz:Envelope xmlns:pasaz="http://schemas.xmlsoap.org/soap/envelope/">
  <pasaz:Body>
    <loadOffers xmlns="urn:ExportB2B">
      <offers><a>abc</a><x>xyz</x></offers>
    </loadOffers>
  </pasaz:Body>
</pasaz:Envelope>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.