Fortunately, PHP5 offers a simple but powerful tool called simpleXML. It has many features that can answer most obstacles we face when we are dealing with XML.
What is XML
XML is a lot like HTML because of that it is easy to create. But, Unlike HTML, you can build and define your tags that match with your scheme. For example that we want to generate an RSS for a list of songs.
We’ll just encode the title of the song, the name of the artist and the last time when the user had played the song.
We make up several tags called <title>, <artist> and <song>. We’ll wrap <title> and <artist> around a <song> tag. We’ll also create a attribute called date for each song. The RSS that we build should something like this.
<songs> <song date="2017-04-25 16:30:73"> <title>Living on Prayer</title> <artist>Bon Jovi</artist> </song> <song date="2017-05-46 15:23:44"> <title>Jailhouse Rock</title> <artist>Elvis Presley</artist> </song> <song date="2017-02-34 13:25:51"> <title>Take It Easy</title> <artist>The Eagles</artist> </song> </songs>
Now save this code as a file called “playlist.xml.”
How to parse XML with simpleXML
After you have created an above XML, you need to parse it. Therefore PHP can read and manipulate every single node in this RSS. There are other methods to parse XML like PHP’s DOM, but the easiest and common one is using “simpleXML”.
Started with simpleXML
Assuming you’ve got that XML file above saved as a file called “playlist.xml”, you can access this file and read the whole file into a PHP object. The following code demonstrates how to read and write out the first artist.
$songs = simplexml_load_file('playlist.xml'); echo $songs->song->artist; //output = Bon Jovi
See that our XML tags now are structurally mapped as part of the object. Therefore we have access to any node simply by typing its name. In this case, “0” represents the first node, and if you want to access the third node simply change it to “2”.
$songs = simplexml_load_file('playlist.xml'); echo $songs->song->artist; //output = The Eagles
After you parsed with simpleXML you can directly obtain every node in this XML. Not only that, if you want, rather than a file, you can change it to URL.
$songs = simplexml_load_file('http://yourdomain.com/playlist.xml'); echo $songs->song->artist; //output = The Eagles
Though I do not recommend, it will be better-using a curl() then send it via variable so it can be used in simpleXML.
Working with Attributes
Now how to get a date, remember we stored it with Attributes. It is slightly different than when we were working with tags. Basically, it works just like accessing a PHP array. Now we are going to get a date in the third song.
$songs = simplexml_load_file('http://yourdomain.com/playlist.xml'); echo $songs->song['date'] //output = 2017-04-25 16:30:73;
SimpleXML is a powerful and easy tool to parse XML files such as blog’s feed or simple API. But if you somehow need a better tool, I highly recommend using PHP dom. A bit complicated but can do more than SimpleXML. Both of them using libxml2, which is used internally by SimpleXML and DOM.
But if you want just parse simple XML. There is no reason at all to use DOM. SimpleXML is just more enough to read every node in XML.