×


How to get data from XML file in PHP

In this article, you will learn how to get data from an XML file using the PHP programming language.

XML(Extensible Markup Language) is the best choice to use in web services and data transport. It is widely used in web applications. We can easily render data stored in XML, as it stores data in a format that is easy to search for and understand. It makes it easier for an informative application to store, retrieve, and display data. It is widely used in business-to-business transactions, generating metadata, e-commerce applications, and so on.









PHP SimpleXML Parser

PHP provides a set of methods that help with XML creation and manipulation. By using the SimpleXML extension, we can easily get XML data. The XML data is returned from the web services, and with the help of SimpleXML, we can easily parse the data.

Simple XML Manipulation In PHP


Suppose the web service returns the following student data in an XML file, 'students.xml'.


<?xml version="1.0" encoding="iso-8859-1"?>
<students>
<student>
<firstname>John</firstname>
<lastname>Player</lastname>
<age>12</age>
<class>5</class>
</student>
<student>
<firstname>Smith</firstname>
<lastname>Soy</lastname>
<age>11</age>
<class>4</class>
</student>
</students> 








PHP read XML data

PHP simplexml_load_file()

The simplexml_load_file() function of PHP interprets an XML file into an object. It returns an object with properties containing the data held within the XML document. We can access any element from the XML by using this object. In the given example, we have used the foreach method to iterate through the entire XML file and read elements from XML.


<?php
$studentdata = simplexml_load_file('students.xml');
if(!$studentdata){
    echo 'Fail to load XML file';
}
else{
    foreach($studentdata as $student){
        echo 'Firstname: '.$student->firstname.'<br/>';
        echo 'Lastname: '.$student->lastname.'<br/>';
        echo 'Age: '.$student->age.'<br/>';
        echo 'Class: '.$student->class.'<br/><br/>';
    }
}
?>    

When you run the above code in the browser, it returns the data in the given format.

Firstname: John 
Lastname: Player
Age: 12
Class: 5

Firstname: Smith
Lastname: Soy
Age: 11
Class: 4




Related Articles

PHP reverse a string without predefined function
PHP random quote generator
PHP convert string into an array
PHP remove HTML and PHP tags from string
Upload multiple files and store in MySQL database using PHP
multiple choice quiz in PHP and MySQL
Import Data Into MySQL From Excel File
Preventing Cross Site Request Forgeries(CSRF) in PHP
PHP code to send email using SMTP
Simple pagination in PHP
Simple PHP File Cache
PHP Connection and File Handling on FTP Server
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP




Read more articles


General Knowledge



Learn Popular Language