Parse XML in PHP

In this tutorial ,we will learn how to parse a XML file in php with some cool and easy example.

What is XML file?

An XML(Extensible Markup Language) file is a text based file format used to store and transport structured data.XML is commonly used for data exchange between system ,Configuration files .XML helps to represent complex data structure in a standardized way.

Characteristic of XML file

  • XML is a flexible data format.
  • XML files can be read and processed on any platform.
  • XML files are also human readable.
  • XML files can be easily parsed and processed by computers.

Creating XML file

<Library>
    <Book>
        <name>Macbeth </name>
        <author>William Shakespeare</author>
        <price>385</price>
    </Book>
    <Book>
        <name>Kabuliwala </name>
        <author>Rabindranath Tagore </author>
        <price>140</price>
    </Book>
    <Book>
        <name>Angry River </name>
        <author>Ruskin Bond</author>
        <price>130</price>
    </Book>
</Library>

Explanation

  • <Library>:It is a root element. This element contain all information about all books.
  • <Book>:This is child element. It contains information about every single book like book name, price etc.
  • <name>:This tag indicates title of the book.
  • <author>: This tag contains the name of the person who wrote the book.
  • <price>:This tag contains price of the book.

What is PHP?

 

PHP is the popular server-side scripting language designed for web development ,though it can also be used as a general purpose language The name PHP stands for “Hypertext preprocessor”. Some key  features of PHP are server side execution, embedded in HTML, data integration.

Parsing XML in a PHP file

<?php
$x=simplexml_load_file("data.xml");
$var=$x->Book;
foreach($var as $displayname){
    foreach($displayname->author as $shows){
        echo "$shows"."<br>";
    }
}
?>

Explanation

  • <?php ?>:This syntax indicates it is a PHP file.
  • $x=simplexml_load_file(“data.xml”): The $x variable is used to store the content of the file “data .Xml” with a help of the function simplexml_load_file.
  • $var=$x->Book:$var is used to store the element of ”book” from the xml file .
  • foreach($var as $displayname): This line starts a for each loop that iterates over each <book> element stored in $var. For each <book> element ,it assigns it to the variable $displayname.
  • foreach($displayname->author as $shows): Inside the first loop thise line starts another foreach loop that iterates over each <author> element within the current book element ($displayname). Each authored is assigned to the variable $shows.
  • echo “$shows”.”<br>”:This line outputs the content of each <author> element.The echo statement prints the value of $shows(which is the author’s name) followed by an HTML line break(<br>),which makes sure that each author appears on a new line in the browser.

Output:

Conclusion

The PHP code loads an XML file containing book information and extracts each <author> element from the <book> elements. It then iterates through these authors and display their names, each on a new line. This code effectively processes and presents authored information from a structured XML file.

Have a happy and great coding!

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top