Understanding XML Parsing using PHP

Understanding XML Parsing using PHP

In the previous tutorial, we have seen how to use JSON in PHP. In this tutorial we’ll see how to parse XML and read its content one by one, we’ll also gonna see how to send an XML to Server-side using AJAX. So before going into understanding XML Parsing using PHP, we’ll see the history of XML, its feature, advantages, and disadvantages.

XML stands for EXtensible Markup Language which is a markup language that is both human-readable and machine-readable.

So, XML is derived from SGML (Standard Generalized Markup Language) and it was developed back in 1970. XML was developed to solve the problem of data interchange between dissimilar systems. This is the reason because of which before JSON was so popular in data exchange and data transfer between client and server.

Now, let’s jump into understanding of parsing XML using PHP.

PHP has provided inbuilt function simplexml_load_string() to process XML and fetch information from it. This function was introduced in PHP in version 5.

simplexml_load_string ( string $data [, string $class_name = “SimpleXMLElement”  [, int $options = 0 [, string $ns = “” [, bool $is_prefix = FALSE ]]]] ) : SimpleXMLElement

Where-
string $data : XML data in string format.
string $class_name : optional parameter. It will return an object of the specified class (if any).
int $options : Optional parameter. It specifies additional Libxml parameters.
string $ns : Optional parameter. It specifies a namespace prefix or URI
bool $is_prefix : Optional parameter. Returns TRUE if ns is a prefix, FALSE if ns is a URI. Default is FALSE.

Now, lets see the format of XML file.

<?xml version="1.0" encoding="UTF-8"?>
<userInfo>
   <name>Kevin</name>
   <age>35</age>
   <address>NJ, USA</address>
   <employment>Self Employement</employment>
</userInfo>

Like in PHP, script start by <?php. So similar to that in XML, script starts with <?xml ?> . Inside the script tag, we can define version of the XML and encoding type.

Next, we have defined our main XML node userInfo and all the other sub-nodes are defined inside it like name, age, address, employment

So, this was the basic example of how XML looks like but in practical example it becomes more complex. But good news is everything looks under nodes so it becomes easy to read.

Following are the list of questions that came across while working on XML.

Parse XML in PHP OR XML PHP parser

To parse XML, PHP has provided an inbuilt extension, SimpleXML. This library is useful to convert XML into an object that can be processed with normal property selectors and array iterators.

To ensure that SimpleXML is enabled, check the phpinfo() function and search for SimpleXML. This extension is enabled by default. It may be disabled by using –disable-simplexml at compile time.

Before PHP 5.1.2, to enable this extension, simply use –enable-simplexml at compile time.

Note: There are no configuration directives defined in php.ini for this extension.

How to use SimpleXML?

Here is the basic snippet about how to use SimpleXML in the code-

<?php

$employeeInfo = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<user>
   <userInfo>
      <name>Kevin</name>
      <age>35</age>
      <address>NJ, USA</address>
      <employment>Self Employement</employment>
   </userInfo>
</user>
XML;

$info = new SimpleXMLElement($employeeInfo);

print_r($info->userInfo[0]->name);

First, we defined XML and assigned it to a variable using Heredoc. Then, we create an object of SimpleXMLElement and passed $employeeInfo variable (which contains XML) as a parameter. Once object of this XML is created then we can access whichever node we want to. We can also loop though it if that is repetitive node.

Convert XML to an array in PHP

Let’s define out XML into a separate file and then access that XML into a separate file using PHP function and we will what different options are, to convert it into an array-

// employee.xml

<?xml version="1.0" encoding="UTF-8"?>
<userInfo>
   <name>Kevin</name>
   <age>35</age>
   <address>NJ, USA</address>
   <employment>Self Employement</employment>
</userInfo>
$path = "employee.xml"; 

// Read XML file as a string
$xmlData = file_get_contents($path); 

// Convert xml string into an object 
$xmlObject = simplexml_load_string($xmlData);

To get data from the XML file in PHP, we can use above simple example.
Now, first, we have converted our XML file data into a string using file_get_contents() and that string is passed to simplexml_load_string() which is PHP function to interprets a string of XML into an object. This will return SimpleXMLElement Object (which we saw in the above example).

We’ve Object and now we can convert it into an array using following ways-

  • Using json_decode() function
  • Typecast into an array using (array)
Using json_decode() function

To convert an XML into an array, PHP has provided its an inbuilt function which is json_decode(). I know you may be confused with the function because we are converting XML into an array and why should I use JSON function.

This is because this is one of the option to convert it into an array. As we get SimpleXMLElement object we can now have to convert it into JSON and for that, we’ll be using json_encode() function and then we can easily convert that JSON into an array using json_decode().

<?php

// Convert XML object into json 
$jsonObject = json_encode($xmlObject); 
  
// Convert JSON into an associative array 
$dataArray = json_decode($jsonObject, true); 
  
print_r($dataArray); 
// OUTPUT

Array ( [name] => Kevin [age] => 35 [address] => NJ, USA [employment] => Self Employement )

This will convert our XML object into JSON and then to an array.
This way we can achieve two targets. 1) use converted JSON. 2) use an array to process the data.

Typecast into an array using (array)

This is the easiest way to convert XML to an array because it will directly convert XML into an array without a need of converting into a JSON string and then using json_decode() to convert it into an array. This will not only satisfy our requirement but also save much function call and memory overhead.

$xmlToarray = (array) $xmlObject;
// OUTPUT

Array ( [name] => Kevin [age] => 35 [address] => NJ, USA [employment] => Self Employement )

Conclusion:

Using SimpleXML library gives us an option to use an inbuilt library which is pre-compiled and we just need to use already existing function to perform different options on given XML.