How to convert array to SimpleXML in PHP
Many times need to store the data as a XML format into the database or into the file for later use. To fulfill this requirement need to convert data to XML and save XML file.
The SimpleXML extension functions provides the tool set to convert XML to an object. Those objects deals with normal property selectors and array iterators.
Example 1:
<?php // Code to convert php array to xml document // Define a function that converts array to xml. function arrayToXml( $array , $rootElement = null, $xml = null) { $_xml = $xml ; // If there is no Root Element then insert root if ( $_xml === null) { $_xml = new SimpleXMLElement( $rootElement !== null ? $rootElement : '<root/>' ); } // Visit all key value pair foreach ( $array as $k => $v ) { // If there is nested array then if ( is_array ( $v )) { // Call function for nested array arrayToXml( $v , $k , $_xml ->addChild( $k )); } else { // Simply add child element. $_xml ->addChild( $k , $v ); } } return $_xml ->asXML(); } // Creating an array for demo $my_array = array ( 'name' => 'GFG' , 'subject' => 'CS' , // Creating nested array. 'contact_info' => array ( 'city' => 'Noida' , 'state' => 'UP' , 'email' => 'feedback@geeksforgeeks.org' ), ); // Calling arrayToxml Function and printing the result echo arrayToXml( $my_array ); ?> |
Output:
<?xml version="1.0"?> <root> <name> GFG </name> <subject> CS </subject> <contact_info > <city > Noida < /city > <state > UP < /state > <email > feedback@geeksforgeeks.org </email> <contact_info> <root>
The above problem ca be solved using array_walk_recursive() function. This function converts array to xml document where keys of array are converted into values and values of array are converted into element of xml.
Example 2:
<?php // Code to convert php array to xml document // Creating an array $my_array = array ( 'a' => 'x' , 'b' => 'y' , // creating nested array 'another_array' => array ( 'c' => 'z' , ), ); // This function create a xml object with element root. $xml = new SimpleXMLElement( '<root/>' ); // This function resursively added element // of array to xml document array_walk_recursive ( $my_array , array ( $xml , 'addChild' )); // This function prints xml document. print $xml ->asXML(); ?> |
Output:
<?xml version="1.0"? > <root > <x> a </x > <y> b </y > <z> c </z > </root >
Note: If the system generate error of type :PHP Fatal error: Uncaught Error: Class ‘SimpleXMLElement’ not found in /home/6bc5567266b35ae3e76d84307e5bdc78.php:24 then simply install php-xml, php-simplexml packages.
Recommended Posts:
- How to convert XML file into array in PHP?
- How to Convert Array to Set in JavaScript?
- How to convert Set to Array in JavaScript?
- How to convert an array into object using stdClass() in PHP?
- How to convert Map keys to an array in JavaScript ?
- How to convert PHP array to JavaScript or JSON ?
- Convert an Array to an Object in JavaScript
- How to convert an Array to String in Java?
- Convert an object to associative array in PHP
- Convert multidimensional array to XML file in PHP
- How to convert array values to lowercase in PHP ?
- How to convert an Object {} to an Array [] of key-value pairs in JavaScript?
- How to convert arguments object into an array in JavaScript ?
- How to convert list of elements in an array using jQuery ?
- Convert a String to Character array in Java
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.