How to parse XML using php simplexml library

Spread the love

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.

php xml parsing with simplexml

This data is both human and machine-readable. It is commonly used for data interchange over the internet. 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.


parse XML using php simplexml library

<?xml version="1.0"?>


   <book id="book1">

      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>




      <description>An in-depth look at creating applications with XML.</description>

   <book id="book2">
       <author>Ralls, Kim</author>
       <title>Midnight Rain</title>
       <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood 
                   to become queen of the world.</description>
   <book id="book3">
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
      <description>The two daughters of Maeve, half-sisters, battle one another for control of England. 
                   Sequel to Oberon's Legacy.</description>

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.


$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">
                  <th>Publish Date</th>

$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>



$booksList .= '</table>';

echo $booksList;


Code explanation

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.

Parse XML using php simplexml

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.

Parse XML using php - source code

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.

Related Tutorials:


Previous Tutorial:


Next Tutorial:


Check Also

generate a pdf using php

How to Generate a PDF using php

Spread the loveLast updated:26th April, 2021How to Generate a PDF using php In the modern …