Top Tip.  If you have an XML document which uses varying XML Namespaces and you want to navigate it using PHP's SimpleXML, you'll need to use 'xpath' to locate your starting node.

<?xml version="1.0"?>

<ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/">

<ns0:Header>

    <AccountInfoHeader xmlns="http://services.hnseu.com">

        <UserName xmlns="">someuser</UserName>

        <Password xmlns="">sompass</Password>

    </AccountInfoHeader>

</ns0:Header>

<ns0:Body>

    <CaseUpdatesRequest xmlns="http://services.hnseu.com">

        <Timestamp xmlns="">2013-07-07T00:00:00</Timestamp>

    </CaseUpdatesRequest>

</ns0:Body>

</ns0:Envelope>

If you feed this in to the SimpleXML or SimpleXMLIterator constructors, you will find you are rooted in the Envelope, but it appears to have no children.  You can't navigate to the Body node.

You can feed the "namespace" in to the SimpleXML constructor for the doc, but then you seem to lose the ability to navigate to non-namespaced nodes, and some nodes seem to disappear.

In this example XML, we want to access the Body properties, which are not namespaced.

The trick is to use 'xpath' to get a SimpleXMLIterator rooted at Body.  The object returned will be able to see everything underneath in a nice, easy, SimpleXML manner.

$xs = new SimpleXMLIterator($xml);

$bod = $xs->xpath("//ns0:Body");

$upd = $bod[0]->CaseUpdatesRequest;

$bod now represents the Body node, and you can use SimpleXML style addressing to navigate down from this node.

Enjoy.




...Click for More
Article
IT
php
Programming
SimpleXML
SOAP
XPath