XML Parsing with SimpleXML



XML Parsing basically means navigate XML document and provide the relevant data. Web services normally return data in XML format, you need to parse XML if you want to full use of APIs. In php 5.0 we have SimpleXML extension that provides a simple way to get XML element’s name and text. SimpleXML is fast and easy to use to read/extract data from XML strings. In this article I will show you XML Parsing with SimpleXML.

So we have sample XML file books.xml


<?xml version="1.0" encoding="UTF-8"?>
<Books>
    <book category="Computer">
    <title>XML Developer's Guide</title>
    <author>Gambardella, Matthew</author>
    <year>2000    </year>
</book>
<book category="children">
    <title>Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
</book>
<book category="Horror">
    <title>Creepy Crawlies</title>
    <author>Knorr</author>
    <author>Stefan</author>
    <year>2003</year>
</book>
</Books>

Above XML file has a list of books and each book has title, author and year of publish.

First step is to load the XML and we have two methods simplexml_load_file() which load the XML file and simplexml_load_string() which load XML from given string.

 $books=simplexml_load_file('books.xml'); 

OR

$xmlstr = file_get_contents('books.xml');
$books=simplexml_load_string($xmlstr);

Both function converts the XML document (or XML string) into an object and stored into $books variable. You can use print_r or var_dump that will return SimpleXMLElement Object representation of the object like as follow.

SimpleXMLElement Object
(
    [book] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [category] => Computer
                        )

                    [title] => XML Developer's Guide
                    [author] => Gambardella, Matthew
                    [year] => 2000
                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [category] => children
                        )

                    [title] => Harry Potter
                    [author] => J K. Rowling
                    [year] => 2005
                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [category] => Horror
                        )

                    [title] => Creepy Crawlies
                    [author] => Array
                        (
                            [0] => Knorr
                            [1] => Stefan
                        )

                    [year] => 2003
                )
        )
)

The XML contained a root element named books, which has three book elements, by this reason the SimpleXMLElement has a property book which is an array of three SimpleXMLElements. Each element of the array corresponds to a book element in the XML document.

You can access the properties of the object with the -> operator. For example, $books->book[0] will give you the first book element object, which has three public properties title, author and year.

echo $books->book[0]->title;
echo $books->book[0]->author;
echo $books->book[0]->year;

you can get the attribute “category” of the book in a simple way like as

 $books->book[0]["category"];

You can iterate to get the list of books to show their details by using a standard looping method, like as foreach.

<?php foreach ($books->book as $book) { ?>
	<div>
		<p>Title : <?php echo $book->title; ?></p>
		<p>Category : <?php echo $book["category"]; ?></p>
		<p>Author : <?php echo $book->author; ?></p>
		<p>Published On : <?php echo $book->year; ?></p>
	</div>
<?php } ?>

Now you know XML Parsing with SimpleXML is very simple and easy. You do more practice with different types of xml feeds and To learn more about SimpleXML check the documentation.

xml parsing with simplexml
xml parsing with simplexml