How to convert XML file into array in PHP?

Given an XML document and the task is to convert an XML file into PHP array. To convert the XML document into PHP array, some PHP functions are used which are listed below:

  • file_get_contents() function: The file_get_contents() function is used to read a file as string. This function uses memory mapping techniques which are supported by the server and thus enhances the performance by making it a preferred way of reading contents of a file.
  • simplexml_load_string() function: Sometimes there is a need of parsing XML data in PHP. There are a handful of methods available to parse XML data. SimpleXML is one of them. Parsing an XML document means that navigating through the XML document and return the relevant pieces of information. Nowadays, a few APIs return data in JSON format but there are still a large number of websites which returns data in XML format. So we have to master in parsing an XML document if we want to feast on APIs available.
  • json_encode() function: The json_encode() function is used to encode a JSON string and return the JSON representation of value.
  • json_decode() function: The json_decode() function is used to decode a JSON string. It converts a JSON encoded string into a PHP variable.

Step 1: Creating an XML file (Optional): Create an XML file which need to convert into the array.
GFG.xml

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version='1.0'?>  
<firstnamedb>  
    <firstname name='Akshat'>  
        <symbol>AK</symbol>  
        <code>A</code>  
    </firstname>  
    <firstname name='Sanjay'>  
        <symbol>SA</symbol>  
        <code>B</code>  
    </firstname>
    <firstname name='Parvez'>  
        <symbol>PA</symbol>  
        <code>C</code>  
    </firstname>
</firstnamedb>

chevron_right


Step 2: Convert the file into string: XML file will import into PHP using file_get_contents() function which read the entire file as a string and store into a variable.



Step 3: Convert the string into an Object: Convert the string into an object which can be easily done through some inbuilt functions simplexml_load_string() of PHP.

Step 4: Convert the object into JSON: The json_encode() function is used to encode a JSON string and return the JSON representation of value.

Step 5: Decoding the JSON Object: The json_decode() function decode a JSON string. It converts a JSON encoded string into a PHP variable.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// xml file path
$path = "GFG.xml";
  
// Read entire file into string
$xmlfile = file_get_contents($path);
  
// Convert xml string into an object
$new = simplexml_load_string($xmlfile);
  
// Convert into json
$con = json_encode($new);
  
// Convert into associative array
$newArr = json_decode($con, true);
  
print_r($newArr);
  
?>

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : SohomPramanick