Convert array to XML in PHP

Many times we need store the data as a XML into the database or into the file for later use. For fulfil this need, we will need to convert the data to XML and save the XML file. In this tutorial we will discuss, how to create XML from array in PHP. We have created a simple script for convert PHP array to XML. You can easily generate XML file from PHP array and save the XML file. You can convert all types of array like Associative array or Multidimensional arrays.

PHP Array:

At first we will store the users data into a variable ($users_array).

$users_array = array(
    
"total_users" => 3,
    
"users" => array(
        array(
            
"id" => 1,
            
"name" => "Smith",
            
"address" => array(
                
"country" => "United Kingdom",
                
"city" => "London",
                
"zip" => 56789,
            )
        ),
        array(
            
"id" => 2,
            
"name" => "John",
            
"address" => array(
                
"country" => "USA",
                
"city" => "Newyork",
                
"zip" => "NY1234",
            ) 
        ),
        array(
            
"id" => 3,
            
"name" => "Viktor",
            
"address" => array(
                
"country" => "Australia",
                
"city" => "Sydney",
                
"zip" => 123456,
            ) 
        ),
    )
);

Array to XML:

Now we will convert the users array to XML using PHP SimpleXML. Please follow the comment tags for better understand.

//function defination to convert array to xml
function array_to_xml($array, &$xml_user_info) {
    foreach(
$array as $key => $value) {
        if(
is_array($value)) {
            if(!
is_numeric($key)){
                
$subnode $xml_user_info->addChild("$key");
                
array_to_xml($value$subnode);
            }else{
                
$subnode $xml_user_info->addChild("item$key");
                
array_to_xml($value$subnode);
            }
        }else {
            
$xml_user_info->addChild("$key",htmlspecialchars("$value"));
        }
    }
}

//creating object of SimpleXMLElement
$xml_user_info = new SimpleXMLElement("<?xml version=\"1.0\"?><user_info></user_info>");

//function call to convert array to xml
array_to_xml($users_array,$xml_user_info);

//saving generated xml file
$xml_file $xml_user_info->asXML('users.xml');
//success and error message based on xml creation
if($xml_file){
    echo 
'XML file have been generated successfully.';
}else{
    echo 
'XML file generation error.';
}

XML File:

The users.xml file contains the following xml.

<?xml version="1.0"?>
<user_info>
    <total_users>3</total_users>
    <users>
        <item0>
            <id>1</id>
            <name>Smith</name>
            <address>
                <country>United Kingdom</country>
                <city>London</city>
                <zip>56789</zip>
            </address>
        </item0>
        <item1>
            <id>2</id>
            <name>John</name>
            <address>
                <country>USA</country>
                <city>Newyork</city>
                <zip>NY1234</zip>
            </address>
        </item1>
        <item2>
            <id>3</id>
            <name>Viktor</name>
            <address>
                <country>Australia</country>
                <city>Sydney</city>
                <zip>123456</zip>
            </address>
        </item2>
    </users>
</user_info>

Insert XML Into Databse

If you want to save the XML into the database, then replace the $xml_file variable line with the following code line. Now you can insert $xml_file variable into the database.

$xml_file $xml_user_info->asXML();

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

3 Comments

  1. Baba Said...
    • CodexWorld Said...
  2. Samantha Machado Said...

Leave a reply

keyboard_double_arrow_up