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.
Want to learn parse XML in php by watching a video?
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”?> .
Create a project folder
Open root folder of your XAMPP or WAMP and add a folder parse-xml-using-php.
Sample XML document
An example XML document is as follows. Create an XML file in project folder books.xml and add the code below into it.
<?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 has 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
Create a file index.php and add code below for parsing an 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.
In this tutorial, you have learned to parse XML using PHP SimpleXML library. You can find source code and example XML file on Github. Click this link to download or clone git repo on your system.
In order to run this script. Copy the project folder in your XAMPP or WAMP root or www folder. Open browser and type URL: http://localhost/parse-xml-using-php/
Please leave your valuable feedback and comments and thanks for reading.
- Parse XML using NodeJS
- Generate XML using PHP and MySQL
- Generate XML using NodeJS and MySQL
- Generate RSS Feed using NodeJS
- Generate PDF using NodeJS