Parse XML with namespace by SimpleXML in PHP

The XML send by outbound message from Salesforce comes with namespace which I cannot parse as most XML tags by SimpleXML.

When google for related solution, it seems children might be the solution, but I cannot find an example that match my case. There are other potential solution like xpath, SoapServer, XML Parser, XMLReader, and even Toolkit for PHP. But I preferred to keep it simple, and focus on SimpleXML.

After test different combination several times, I finally make it work for my case. Here is how:

Basic rules of children() and namesapce

For XML tags, if

  1. the name comes with namespace, use children(‘namespace’) before the tag.
  2. the name doesn’t comes with namespace, use the tag name.
  3. the tag contains a xmlns, use children(‘namespace’) before the tag.
  4. the tag contains a definition of a namespace by xmlns, the namespace might be references in children later.

It’s difficult to understand above rules. I will show you an example to help.

XML with namespace from Salesforce Outbound Message

Here is the sample xml file we need to use. It is generated by Salesforce Outbound Message and has many namespace in these tags.

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="" xmlns:xsd="" xmlns:xsi="">
  <notifications xmlns="">
   <SessionId xsi:nil="true"/>
    <sObject xsi:type="sf:Lead" xmlns:sf="">

Parse XML with namespace for tag value

I get the filed value of CreatedDate by following code fragment.

$rcXML is the object of the interpreted XML file by simplexml_load_string(). What I need to access is in between of <sf:CreatedDate> and </sf:CreatedDate>.

First, I need to drill down from begin. $rcXML->children(‘;)->Body point to the block between <soapenv:Body> and </soapenv:Body>. The definition of soapenv is “; which may be found in the second line of the xml file as xmlns:soapenv=”;. If you use “soapenv” in children() as mentioned in the post in StackOverflow, it won’t work. You should follow the syntax in this article by Kevin Yank.

Next, $rcXML->children(‘;)->Body->children(‘;)->notifications point to the block between <notifications xmlns=””&gt; and </notifications>. The xmlns=”; means it is the default namespace for notifications. That’s why we have to use children().

$rcXML->children(‘;)->Body->children(‘;)->notifications->Notification point to the block between <Notification> and </Notification>. You may get the value of <Id> by $rcXML->children(‘;)->Body->children(‘;)->notifications->Notification->Id

$rcXML->children(‘;)->Body->children(‘;)->notifications->Notification->sObject point to the block between <sObject xsi:type=”sf:Lead” xmlns:sf=””> and </sObject>. It contains namespace definition for sf and is not the default namespace, we don’t need to use children() before sObject. BTW, urn is Universal Resource Name, Some names space use it instead of url which means Uniform Resource Locator.

Last, $rcXML->children(‘;)->Body->children(‘;)->notifications->Notification->sObject->children(‘’)->CreatedDate point to the block between <sf:CreatedDate> and </sf:CreatedDate>. sd, the namespace, is defined in <sObject>, we need to use children and its urn to be able to access the value in <sf:CreatedDate>.

Test and Run

The full program as below. You need to save the xml files as the one I used in my program “OutboundMessage.xml” and put them in the same directory to make it work.

header("Content-Type: text/plain\r\n");

$xmlFile = 'OutboundMessage.xml';

$file = fopen( $xmlFile, 'r' );
$content = fread( $file, filesize($xmlFile) );
fclose( $file );

echo "XML ==============================".PHP_EOL.PHP_EOL;
echo $content;

echo PHP_EOL."Parsing ..............................".PHP_EOL.PHP_EOL;

$rcXML = simplexml_load_string($content);

echo '<Id>: '.$rcXML->children('')->Body->children('')->notifications->Notification->Id.PHP_EOL;

echo '<sf:Id>: '.$rcXML->children('')->Body->children('')->notifications->Notification->sObject->children('')->Id.PHP_EOL;

echo '<sf:CreatedDate>: '.$rcXML->children('')->Body->children('')->notifications->Notification->sObject->children('')->CreatedDate.PHP_EOL;

echo '<sf:LastName>: '.$rcXML->children('')->Body->children('')->notifications->Notification->sObject->children('')->LastName.PHP_EOL;

echo PHP_EOL."End ==============================".PHP_EOL.PHP_EOL;
XML with namespace parsed by SimpleXML
XML with namespace parsed by SimpleXML


  1. SimpleXML
  2. SimpleXMLElement::children
  3. SimpleXMLElement::xpath
  4. The SoapServer class
  5. XML Parser
  6. XMLReader
  7. Salesforce: Toolkit for PHP (Version 20.0)
  8. simplexml_load_string()
  9. StackOverflow: simplexml help how do I parse this?
  10. SitePoint: SimpleXML and namespaces by Kevin Yank
  11. XML Namespaces – Default Namespaces
  12. XML Namespaces – Uniform Resource Identifier (URI)


6 thoughts on “Parse XML with namespace by SimpleXML in PHP

  1. This article looks promising compared to the rest I’ve been seeing until I tried it on my own and won’t get the desired result.

    I’ll appreciate if you can help write the code to extract ReferenceNumber from this XML


    Thanks in anticipation.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s