How to Get Data from XML Files in PHP
  John Mwaniki /   16 Dec 2023

How to Get Data from XML Files in PHP

XML (eXtensible Markup Language) is a markup language and human-readable data format used for storing and transmitting structured data.

Like HTML, XML consists of information enclosed within opening and closing tags. However, unlike HTML, XML is designed to store and carry data but not to display it.

Its tags are not predefined by XML standards but rather created by the author of the XML document in a self-descriptive manner.

Example

<student>
  <name>John Doe</name>
  <age>21 yrs</age>
  <gender>Male</gender>
  <course>Computer Science</course>
</student>

In this example, the <student> element contains four child elements which include <name>, <age>, <gender>, and <course> containing the student's information.

Reading XML Data in PHP

PHP provides several methods to read XML files. In this article, we will cover how to do it with SimpleXML.

SimpleXML is a built-in PHP extension for reading and manipulating XML files.

It transforms XML documents into objects and provides an easy way of accessing and getting an element's name, attributes, and textual content if you know the XML data structure.

The PHP simplexml_load_file() function converts an XML document to an object and returns a SimpleXMLElement object on success (which we can store in a variable) or false on failure.

Example

$xml = simplexml_load_file("path/to/file.xml");

The PHP simplexml_load_string() function converts an XML string into an object and returns a SimpleXMLElement object on success or false on failure.

Example

<?php

$student = '
<student>
  <name>John Doe</name>
  <age>21 yrs</age>
  <gender>Male</gender>
  <course>Computer Science</course>
</student>';

$xml = simplexml_load_string($student);
print_r($xml);
?>

Output:
SimpleXMLElement Object ( [name] => John Doe [age] => 21 yrs [gender] => Male [course] => Computer Science )

The only difference between the two functions is that simplexml_load_file() is used when you want to read data from an XML (.xml) file while simplexml_load_string() is used when reading data from an XML string. Everything else after that is the same.

Retrieving specific data is as simple as accessing object properties. We use the arrow operator (->) on the SimpleXMLElement object followed by the element name as the property.

Example

<?php

$student = '
<student>
  <name>John Doe</name>
  <age>21 yrs</age>
  <gender>Male</gender>
  <course>Computer Science</course>
</student>';

$xml = simplexml_load_string($student);
echo $xml-> name."<br>";
echo $xml->age."<br>";
echo $xml->gender."<br>";
echo $xml->course;
?>

Output:
John Doe
21 yrs
Male
Computer Science

We may have a scenario where the XML document consists of an array of elements, e.g., multiple students in our case.

<classStudents>
  <student>
    <name>John Doe</name>
    <age>21 yrs</age>
    <gender>Male</gender>
    <course>Computer Science</course>
  </student>
  <student>
    <name>Ranchoddas Chanchad</name>
    <age>25 yrs</age>
    <gender>Male</gender>
    <course>Software Engineering</course>
  </student>
  <student>
    <name>Raju Rastogi</name>
    <age>24 yrs</age>
    <gender>Male</gender>
    <course>Information Technology</course>
  </student>
  <student>
    <name>Farhan Qureshi</name>
    <age>26 yrs</age>
    <gender>Male</gender>
    <course>Computer Engineering</course>
  </student>
</classStudents>

In such a case, you can use the foreach loop iterates through the students' data, and then inside the loop, we can access the details of each student.

Example

<?php

$students = simplexml_load_file("students.xml");
foreach ($students as $student) {
    echo $student -> name . ": " . $student -> age . ", " . $student -> gender . ", " . $student -> course . "<br>";
}
?>

Output:
John Doe: 21 yrs, Male, Computer Science
Ranchoddas Chanchad: 25 yrs, Male, Software Engineering
Raju Rastogi: 24 yrs, Male, Information Technology
Farhan Qureshi: 26 yrs, Male, Computer Engineering