
When we run the above program, we get the following output − In the below example we get the structure of the feed so that we can analyse further about which parts of the feed we want to process. In python we take help of the below package to read and process these feeds. Many news-related sites, weblogs and other online publishers syndicate their content as an RSS Feed to whoever wants it. load('books.RSS (Rich Site Summary) is a format for delivering regularly changing web content. This time we want to make elements called author_firstname and author_lastname and then delete author. We do some simple switching around and change the node value for each author element. The value of a node can be accessed through its property nodeValue. The XPath query this time says "get all elements called 'author' which are children of an element called 'book'". Now let's try to parse some real-world data.

You can of course use any valid XPath query. If we wanted to select all the book titles we could do something like this: xpath('book/title') Īll ISBN numbers would be retrieved like this: as $isbn)

They are run using the SimpleXMLElement::xpath() method that method takes a single argument, the XPath query. The ISBN number of the same book is displayed like this: echo $books->book XPath Right, so you say you wanted to display the title of the second book you'd do this (remember that array indices start from 0): echo $books->book->title This will result in a table looking like this: Now let's turn our XML data into an HTML table: įoreach($books as $book) // loop through our books In that case it will look like this: $books = simplexml_load_file('books.xml') If allow_url_fopen is set to true in php.ini then you can specify a URL as well.Īnother way of loading a file is by using the simplexml_load_file() function. The third argument specifies if the first argument is a filename ( true) or actual XML data ( false) and defaults to false. We won't touch that in this tutorial, but you are free to research that yourself. The second argument is used to set certain options. I only used file_get_contents() for the purpose of that example. The first way could be used if you already have some XML data which could for instance be retrieved from some web service. If you have the file stored you should obviously use the latter approach as there is no reason to get the file contents ourselves when we can get SimpleXML to do it for us. Either we can pass a string to SimpleXML or we can tell it where the file is: We can load our data into SimpleXML in two ways. Throughout this tutorial we will use the file books.xml which I created using data from the top three books on 's Editor's Picks: Best Books of 2007. PHP 5 has a class called SimpleXML which is. Right, let's go to the next page and get started. Furthermore, OOP knowledge is a requirement as well. Knowledge of XPath would be a good idea as it will be used a bit in this tutorial, however, this is not entirely important. Additionally we'll create a really simple RSS reader.īasic knowledge of XML is a prerequisite for this tutorial. At the end of the tutorial we will also take a look at how you can create an RSS feed without writing a single line of XML and then we will make it into a reusable class which you can implement in your own applications. With the emerge of PHP 5, support for handling XML data has greatly improved and in this tutorial we will take a look at the features in PHP 5 which we can use to parse, alter and create XML documents. It is often used for sharing data between applications and a common usage of XML is for instance RSS feeds. XML, E xtensible Markup Language, is a general-purpose markup language which can be used for storing arbitrary data in a structured way.

This tutorial was originally posted on the old site on 20th December, 2007.
