SimpleXML as it name imply, is a very simple API to traverse XML implemented specially in PHP language. It is very similar to the XPath, but since it has more PHP friendly syntax PHP developers really like to use it.
As an Example for this XML:
<dwml> <data> <location> <location-key>point1</location-key> <point latitude="37.39" longitude="-122.07"></point> </location> </data> ..... </dwml>
XPATH Query to take the latitude in more general way
Where as with simple XML it is just a familiar PHP statement,
Anyway still you can use the xpath inside your simplexml code. You can execute xpath queries by calling xpath function from any SimpleXMLEelment. It will return an array of SimpleXMLElement that match your query. So for the above example your XPath query would be something like this,
$simplexml= new SimpleXMLElement($xml); $lats = $simplexml->xpath('/dwml/data/location/point/@latitude'); echo $lats;
This simplicity allows you to choose between these two methods interchangeably as best fit per your application. Here are some cases that I think use of XPath is more easy.
Ability to use of XPath shorthand
Take the above example XML it self. If there is only one attribute named ‘latitude’ throughout the XML you can call that value by
If XML node name or attribute name contains characters like ‘-‘ which are not allowed in PHP for variable names
In the example if you want to access the value inside ‘location-key’ node using simplexml it would be like,
This will not give you the expected result as PHP will try to think ‘location’ and ‘key’ as two taken in ‘location-key’. So this particular code can be replaced with the xpath function.
$keys = $simplexml->xpath('/dwml/data/location/location-key'); echo $keys;
You want to iterate through node with a same name in an XML
If the nodes which we want to iterate is in organized positions in an XML (like the one in following) both approaches can be used with same easiness.
<root> <mynode>value1</mynode> <mynode>value2</mynode> <mynode>value3</mynode> <mynode>value4</mynode> <mynode>value5</mynode> </root>
But how if the ‘mynode’ was in different locations in an XML like this,
<root> <anothernode> <mynode>value1</mynode> </anothernode> <anotheranothernode> <anotheranotheranothernode> <mynode>value2</mynode> </anotheranotheranothernode> <mynode>value3</mynode> </anotheranothernode> <mynode>value4</mynode> </root>
You can iterate all the ‘mynode’ nodes with the following xpath query.
Note that this case can be handled easily in DOM with the getElementsByName.
To use the power of XPath functions and Axes
You can use the XPath functions like last(), position() and even string manipulation functions like substring() in a XPath statement.
For an example in the above example, if you want only take the value of last ‘mynode’ just use this expression
And you can use the power of axes in Xpath Queries. If you want to iterate all the ancestors from current node just use this query
Access elements with different namespaces
<saleItems> <ns1:car xmlns:ns1="http:/toyota.xxx.com">$3000</ns1:car> <ns2:car xmlns:ns2="http:/suziki.rrr.com">$4000</ns2:car> </saleItems>
You want to extract the cars from simpleXML. You can do this by following code.
$simplexml= new SimpleXMLElement($xml); $ns1_childs = $simplexml->children("http:/toyota.xxx.com"); echo $ns1_childs->car; $ns2_childs = $simplexml->children("http:/suziki.rrr.com"); echo $ns2_childs->car;
Every time you access a different namespace you have to call the children method with the namespace as an argument.
If you use XPath approach, you first register the namespces with an prefix and just use those prefix in your XPath queries.
$simplexml= new SimpleXMLElement($xml); $simplexml->registerXPathNamespace("p1", "http:/toyota.xxx.com"); $simplexml->registerXPathNamespace("p2", "http:/suziki.rrr.com"); $toyota_cars = $simplexml->xpath('//p1:car'); $suziki_cars = $simplexml->xpath('//p2:car'); echo $toyota_cars; echo $suziki_cars;
SimpleXML is simple and powerful in its native form. But whenever it is impossible or difficult to use you don’t need to go back for tedious DOM or manual string manipulation. You can use the xpath queries to get the work done within the simplexml environment itself.