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="">


    <AccountInfoHeader xmlns="">

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

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




    <CaseUpdatesRequest xmlns="">

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




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.


...Click for More