Convert XML to Array in PHP Using XML2Array

in this post. I am going to tell you about converting XML file data into PHP array, I am using XML2Array libs, I am also letting you know how to convert XML to array without any libs as well.

Converting XML to array is a very common functionality in PHP while working with web services. We are normally using XML type data for requests and responses between server and client communication. I am sharing this tutorial to convert xml to array in php using XML2Array lib class.

I have already shared a post How To Convert XML To Associative Array in PHP, In that post, i am using simplexml_load_string() php function.

But, I am using XML2Array class to convert XML data into PHP array.

What’s XML2Array

The XML2Array is a class that is used to convert XML to an array in PHP. It returns a PHP array that can be converted back to XML using the Array2XML class. This class use string XML as input or an object of type DOMDocument.

As per docs, There are the following Conventions,

  1. Attributes stored as key-value pairs under [‘tag_name’][‘@attributes’]
  2. CDATA nodes are stored under [‘tag_name’][‘@cdata’]
  3. In case a node has attributes, the value will be stored in [‘tag_name’][‘@value’]

Also checkout other related tutorials,

Convert XML into PHP Array Using XML2Array

Let’s create an array from an XML file by following the steps.

Step 1: Download XML2Array class from Here.

Step 2: We are using XML2Array libs to convert xml file data into the PHP array, Let’s download XML2Array library.

include_once("xml2php.php");

Step 3: Now we will create a php variable that will hold xml file content. The following code is defined as a xml_data name variable and assigned xml file content.

$xml_data= "<?xml version="1.0" encoding="UTF-8"?> 
<note> <to>Tove</to>
<from>Jani</from> 
<heading>Reminder</heading> 
Don't forget me this weekend! </note>"

Step 4: Create PHP array data from XML file using the inbuilt createArray() method, This method takes a variable that holds XML file content. We’ll pass XML file as a variable on it.

$array = XML2Array::createArray($xml);

Full Source Code:

include_once("xml2php.php");
$xml_data= "<?xml version="1.0" encoding="UTF-8"?> 
    <note> <to>Tove</to>
    <from>Jani</from> 
    <heading>Reminder</heading> 
    Don't forget me this weekend! </note>"
$array = XML2Array::createArray($xml_data);
print_r($array);

PHP XML to array Using simplexml_load_string()

We can also use the simplexml_load_string() method to transform XML into a PHP array. Using the file_get_contents() method, we’ll read a file and convert it to PHP object data using the simplexml_load_string() method.

Then, using the json_encode() method, we’ll transform the object to JSON data, which will then be converted to an array using the json_decode() method.

<?php
// xml file path or locaton
$path = "test.xml";

// Read entire file into string variable
$xmlfile = file_get_contents($path);

// Convert xml string into an PHP object
$xml_obj = simplexml_load_string($xmlfile);

// Convert into json Data
$json_data = json_encode($xml_obj);
  
// Convert into associative array
$arr_data = json_decode($json_data, true);
print_r($arr_data);
?>

Demo & Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *