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:

Extracted data from XML file



How to Extract Data from an XML File Using PHP ?

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.

Table of Content

  • Sample XML File
  • Extracting Data from XML File

Similar Reads

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

Extracting Data from XML File

...