How to parse XML using php simplexml
In this tutorial we are going to learn how to parse XML using php simplexml. XML stands for eXtensible Markup Language. XML is used to store and transport data.
This data is both human and machine readable. It is commonly used for data interchange over the internet. w3.org is responsible for developing efficient ways to exchange XML docs.
XML document structure
XML documents are descriptive and have tree like structure. Unlike HTML, XML documents have custom tags. It has a root or parent element. Root element have child elements. Child elements can have further child elements. Elements have start and end tags. Inside tags elements can have data and attributes. XML document always starts with <?xml version=“1.0”?> .
Sample XML document
An example XML document is as follows.
<?xml version="1.0"?> <books> <book id="book1"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> <book id="book2"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> </book> <book id="book3"> <author>Corets, Eva</author> <title>The Sundered Grail</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2001-09-10</publish_date> <description>The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to Oberon's Legacy.</description> </book> </books>
XML document explanation.
In above XML file, you can view parent <books> element. There are many <book> items inside root element. Each book element has further child elements like author, title , genre, price, publish date, description. Each book element have an attribute id. child elements like author, title, genre, price, publish_date, description have data between start and end tag.
PHP Code to Parse an XML document
The code below is parsing XML document
<?php $xmlfile = 'books.xml'; $books = simplexml_load_file($xmlfile) or die("Error: Cannot create XML object"); $booksList = '<h2 align="center">Books List from XML Document</h2>'; $booksList .= '<table border="1" align="center" cellpadding="5"> <tr> <th>#</th> <th>Title</th> <th>Author</th> <th>Category</th> <th>Price</th> <th>Publish Date</th> <th>Description</th> </tr>'; $serial = 1; foreach ($books as $bookinfo): $title = $bookinfo->title; $author = $bookinfo->author; $genre = $bookinfo->genre; $price = $bookinfo->price; $pdate = $bookinfo->publish_date; $desc = $bookinfo->description; $booksList .= "<tr> <td>".$serial."</td> <td>".$title."</td> <td>".$author."</td> <td>".$genre."</td> <td>".$price."</td> <td>".$pdate."</td> <td>".$desc."</td> </tr>"; $serial++; endforeach; $booksList .= '</table>'; echo $booksList; ?>
So at first you will use the simplexml_load_file function to load XML document. XML document is loaded into memory in $books variable. We create a variable $booksList and start creating a table with Title, Author, Category, Price, Publish Date and Description headings.
Then in foreach ($books as $bookinfo): loop each book element data such as title, author, category, price, publish_date and description is concatenated with $booksList variable inside a <tr> and <td> elements. After end of foreach loop, XML document parsed information inside $booksList element is displayed. The image below displays books list parsed from XML doc.
Using simplexml library we can easily parse XML document. Please leave your valuable feed back / comments and thanks for reading.