Open In App

How to Extract Data from an XML File Using PHP ?

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

XML, which stands for Extensible Markup Language, is a data storage format that is easily searchable and understandable. It simplifies the process of storing, retrieving, and displaying data in informative applications. It is widely used in web applications. In this article, we will learn how to extract data from an XML file using the PHP programming language.

Sample XML File

Below mentioned XML file contains data about Books. It has all the information that is required by a reader about a book such as author, title, price, genre, etc.

XML




<?xml version="1.0"?>
<catalog>
   <book id="wd101">
      <author>Smith, John</author>
      <title>Mastering HTML5</title>
      <genre>Web Development</genre>
      <price>34.99</price>
      <publish_date>2022-03-15</publish_date>
      <description>
          A comprehensive guide to mastering the
          latest features of HTML5 for web development.
      </description>
   </book>
   <book id="wd102">
      <author>Williams, Sarah</author>
      <title>Responsive Web Design Techniques</title>
      <genre>Web Development</genre>
      <price>28.95</price>
      <publish_date>2021-09-20</publish_date>
      <description>
          Explore effective techniques for creating
          responsive and mobile-friendly web designs.
      </description>
   </book>
   <book id="wd103">
      <author>Anderson, James</author>
      <title>JavaScript Programming Essentials</title>
      <genre>Web Development</genre>
      <price>22.50</price>
      <publish_date>2020-05-08</publish_date>
      <description>
          Essential concepts and techniques for mastering
          JavaScript programming in web development.
      </description>
   </book>
   <book id="wd104">
      <author>Roberts, Emily</author>
      <title>Full Stack Development with Node.js</title>
      <genre>Web Development</genre>
      <price>45.99</price>
      <publish_date>2019-11-12</publish_date>
      <description>
          A comprehensive guide to full-stack web
          development using Node.js, Express, and MongoDB.
      </description>
   </book>
   <book id="wd105">
      <author>Chang, Li</author>
      <title>Modern CSS Frameworks</title>
      <genre>Web Development</genre>
      <price>19.95</price>
      <publish_date>2018-06-25</publish_date>
      <description>
          Learn to leverage the power of modern CSS frameworks
          for efficient and stylish web development.
      </description>
   </book>
</catalog>


Extracting Data from XML File

PHP provides us a various methods that helps with XML creation and manipulation. The XML data is returned from the web services, and with the help of SimpleXML, we can easily parse the data.

Approach:

  • The name of the XML file we want to read and extract data from is stored in $filename variable.
  • Then we use a try-catch block to handle errors properly.
  • Inside the try block, implement the simplexml_load_file() function to load the required XML file and parse it into a SimpleXMLElement object.
  • If there is an error while loading the file or parsing the XML, we throw an exception error.
  • We then extract desired data from the given XML file through the XML element names.
  • Display the final result.

Example: Below mentioned code can be used to extract information from the XML file that is mentioned above. Steps that we should follow to extract data from an XML file:

CSS




.container {
    border: 1px solid #ccc;
    padding: 10px;
    margin-bottom: 10px;
}
 
.info {
    display: flex;
    flex-direction: row;
    gap: 20px;
}
 
.tag {
    color: #666;
    border: 1px solid #ccc;
    padding: 10px;
    border-radius: 10px;
}
 
.heading {
    margin: 0px;
}


PHP




<?php
$filename = "books.xml";
 
try {
      // Loading the required file data
    $xml = simplexml_load_file($filename);
 
    // Iterate over book elements and extract specific data
    foreach ($xml->book as $book) {
        $author = $book->author;
        $title = $book->title;
        $genre = $book->genre;
        $price = $book->price;
        $publish_date = $book->publish_date;
        $description = $book->description;
 
        // Add styling and display book details
        echo "<div class='container'>";
        echo "<h2 class='heading'>$title</h2>";
        echo "<div class='info'>";
        echo "<p class='tag'><b>Author:</b> $author</p>";
        echo "<p class='tag'><b>Genre:</b> $genre</p>";
        echo "<p class='tag'><b>Price:</b> $$price</p>";
        echo "<p class='tag'><b>Publish Date:</b> $publish_date</p>";
        echo "</div>";
        echo "<p class='heading'><b>Description:</b> $description</p>";
        echo "</div>";
    }
} catch (Exception $e) {
    echo "ERROR: " . $e->getMessage();
}
?>
 
<!-- Including CSS file -->
<style>
    <?php include 'style.css'; ?>
</style>


Output:

xml

Extracted data from XML file



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads