How to Extract Data from an XML File Using PHP ? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like 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. Table of Content Sample XML FileExtracting Data from XML FileSample XML FileBelow 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 FilePHP 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 Comment More infoAdvertise with us Next Article How to Extract Data from an XML File Using PHP ? H harshtondak Follow Improve Article Tags : PHP PHP-XML Similar Reads How to generate an XML file dynamically using PHP? A file can be generated using PHP from the database, and it can be done by the static or dynamic method in PHP. Static methods can be called directly - without creating an instance of a class. Here we are going to discuss how to create an XML file dynamically.The first thing we need to do is fetch t 2 min read How to Convert XML data into JSON using PHP ? In this article, we are going to see how to convert XML data into JSON format using PHP. Requirements: XAMPP Server Introduction: PHP stands for hypertext preprocessor, which is used to create dynamic web pages. It also parses the XML and JSON data. XML stands for an extensible markup language in wh 3 min read Why to use extract() function in PHP ? Extract() function is an inbuilt function in PHP which is used when there is conflict in the variable name. When someone writes a code of approximately 100 lines, there is a chance that he declares the variable already then there is a conflict between what value the variable we assign. By using the 5 min read How to parse and process HTML/XML using PHP ? In this article, we will learn how to process XML using PHP. We have already learnt the basics of XML and their differences with respect to HTML. Different elements of XML can be learnt from XML Elements to understand the working of the following programs. The simplexml_load_string() function is use 2 min read PHP | xml_set_character_data_handler() Function The xml_set_character_data_handler() function is an inbuilt function in PHP which is used to set the character data handler function for XML parser. Syntax:Â bool xml_set_character_data_handler( resource $xml_parser, callable $data_handler ) Parameters: This function accepts two parameters as mentio 3 min read PHP | xml_error_string() Function Pre-requisite: XML BasicsThe xml_error_string() function is an inbuilt function in PHP which returns the XML parser error description for generated error code. Syntax:Â string xml_error_string( int $error_code) Parameters: This function accepts single parameter $error_code which is required. It spec 3 min read How to get the source code of a web page using PHP ? Given a webpage, for which we need to find its source code using PHP. For this, we are going to use the PHP htmlspecialchars() function which converts any predefined characters to their subsequent HTML entities.Example 1: Suppose we take a sample website that looks like the below image, let us see w 2 min read PHP | simplexml_load_file() Function The simplexml_load_file() function is an inbuilt function in PHP which is used to convert the well-formed XML document into the given file to an object. Syntax: SimpleXMLElement simplexml_load_file( string $filename, string $class_name = "SimpleXMLElement", int $options = 0, string $ns = "", bool $i 2 min read PHP | SimpleXMLElement addAttribute() Function Pre-requisite: Read XML Basics The SimpleXMLElement::addAttribute() function is an inbuilt function in PHP which add an attribute in a SimpleXML object. Syntax: void SimpleXMLElement::addAttribute($name, $value, $namespace) Parameter: This function accepts three parameters as mentioned above and des 2 min read PHP | DOMCharacterData substringData() Function The DOMCharacterData::substringData() function is an inbuilt function in PHP which is used to extracts a range of data from the node. Syntax: string DOMCharacterData::substringData( int $offset, int $count ) Parameters: This function accept two parameters as mentioned above and described below: $off 2 min read Like