The Wayback Machine - https://web.archive.org/web/20240617171048/https://www.geeksforgeeks.org/how-to-convert-xml-file-into-array-in-php/
Open In App

How to convert XML file into array in PHP?

Last Updated : 02 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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




<?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>


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:




<?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);
  
?>


Output:



Similar Reads

Convert multidimensional array to XML file in PHP
Given a multi-dimensional array and the task is to convert this array into an XML file. To converting the multi-dimensional array into an xml file, create an XML file and use appendChild() and createElement() function to add array element into XML file. Example: First, create a PHP Multidimensional Array for converting that array into the XML file
2 min read
How to Convert XML data into JSON using PHP ?
In this article, we are going to see how to convert XML data into JSON format using PHP. Requirements: XAMPP Server Introduction: PHP stands for hypertext preprocessor, which is used to create dynamic web pages. It also parses the XML and JSON data. XML stands for an extensible markup language in which we can define our own data. Structure of XML:
3 min read
How to include content of a PHP file into another PHP file ?
Including the content of a PHP file into another file reduces the complexity of code by reducing the code and improving its modularity by splitting the code into different files so that it is easy to understand and manage. There are two ways to do it by using the following PHP functions. PHP include() Function: It will show the warning if the file
2 min read
Convert xml data into json using Node.js
XML: Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The design goals of XML focus on simplicity, generality, and usability across the Internet. It is a textual data format with strong support via Unicode for different human langua
2 min read
How to Load XML Data into MySQL using PHP ?
In this article, we are going to store data present in XML file into MySQL database using PHP in XAMPP server. XML: Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The design goals of XML focus on simplicity, generality, and usabil
4 min read
PHP code to store XML data into CSV
In this article, we are going to store XML data into a CSV file using PHPH. XML stands for an extensible markup language. XML is similar to HTML but whereas in HTML, we can't define our own tags, but in XML we can define our own tags. Requirements: XAMPP Server. Structure of XML: &lt;root&gt; &lt;child&gt &lt;subchild&gt;.....&lt;/subchild&gt; &lt;
3 min read
How to generate an XML file dynamically using PHP?
A file can be generated using PHP from the database, and it can be done by the static or dynamic method in PHP. Static methods can be called directly - without creating an instance of a class. Here we are going to discuss how to create an XML file dynamically. The first thing we need to do is fetch the data from the database. For that, we need to w
2 min read
How to Extract Data from an XML File Using PHP ?
XML, which stands for Extensible Markup Language, is a data storage format that is easily searchable and understandable. It simplifies the process of storing, retrieving, and displaying data in informative applications. It is widely used in web applications. In this article, we will learn how to extract data from an XML file using the PHP programmi
3 min read
Convert JSON file into CSV file and displaying the data using Node.js
There are so many ways to store data for better understanding for individual purposes, in few cases, JSON files will be more suitable in few cases, CSV files, and there are also lots of other types as well like XML, etc. In this article, we will convert the JSON file data into CSV file data and also display that through Node.js. JSON stands for Jav
3 min read
How to Convert JSON file into CSV in PHP ?
In this article, we are going to see how to convert JSON data into a CSV file using PHP. JSON (JavaScript Object Notation) is a dictionary-like notation that can be used to structuring data. It is stored with the extension .json, for example - geeksforgeeks.json On the other hand, CSV (or Comma Separated Value) files represent data in a tabular for
2 min read