Convert XML to PHP array

Source: Internet
Author: User

Here is a class to convert the XML to a PHP array, the following is the code for the class

<?php
/**
* Xml2array:a class to convert XML to array in PHP
* It returns the array which can is converted back to XML using the Array2xml script
* It takes an XML string or a DOMDocument object as an input.
*
* See Array2xml:http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes
*
* Author:lalit Patel
* Website:http://www.lalit.org/lab/convert-xml-to-array-in-php-xml2array
* License:apache License 2.0
* http://www.apache.org/licenses/LICENSE-2.0
* version:0.1 (DEC 2011)
* version:0.2 (2012)
* Fixed typo ' DomDocument ' to ' DomDocument '
*
* Usage:
* $array = Xml2array::createarray ($xml);
*/

Class Xml2array {

private static $xml = null;
private static $encoding = ' UTF-8 ';

/**
* Initialize the root XML node [optional]
* @param $version
* @param $encoding
* @param $format _output
*/
public static function init ($version = ' 1.0 ', $encoding = ' UTF-8 ', $format _output = True) {
Self:: $xml = new DOMDocument ($version, $encoding);
Self:: $xml->formatoutput = $format _output;
Self:: $encoding = $encoding;
}

/**
* Convert an XML to Array
* @param string $node _name-name of the root node to be converted
* @param array $arr-aray to be converterd
* @return DOMDocument
*/
public static function &createarray ($input _xml) {
$xml = Self::getxmlroot ();
if (is_string ($input _xml)) {
$parsed = $xml->loadxml ($input _xml);
if (! $parsed) {
throw new Exception (' [Xml2array] Error parsing the XML string. ');
}
} else {
if (Get_class ($input _xml)! = ' DOMDocument ') {
throw new Exception (' [Xml2array] The input XML object should be of type:domdocument. ');
}
$xml = self:: $xml = $input _xml;
}
$array [$xml->documentelement->tagname] = Self::convert ($xml->documentelement);
Self:: $xml = null; Clear the XML node in the class for 2nd time use.
return $array;
}

/**
* Convert an Array to XML
* @param mixed $node-xml as a string or as an object of DOMDocument
* @return Mixed
*/
private static function &convert ($node) {
$output = Array ();

Switch ($node->nodetype) {
Case Xml_cdata_section_node:
$output [' @cdata '] = Trim ($node->textcontent);
Break

Case Xml_text_node:
$output = Trim ($node->textcontent);
Break

Case Xml_element_node:

For each child node, call the covert function recursively
For ($i =0, $m = $node->childnodes->length; $i < $m; $i + +) {
$child = $node->childnodes->item ($i);
$v = Self::convert ($child);
if (Isset ($child->tagname)) {
$t = $child->tagname;

Assume more nodes of same kind is coming
if (!isset ($output [$t])) {
$output [$t] = array ();
}
$output [$t] = $v;
} else {
Check if it isn't an empty text node
if ($v!== ") {
$output = $v;
}
}
}

if (Is_array ($output)) {
If only one node of its kind, assign it directly instead if array ($value);
foreach ($output as $t = = $v) {
if (Is_array ($v) && count ($v) ==1) {
$output [$t] = $v [0];
}
}
if (empty ($output)) {
For empty nodes
$output = ";
}
}

Loop through the attributes and collect them
if ($node->attributes->length) {
$a = array ();
foreach ($node->attributes as $attrName = + $attrNode) {
$a [$attrName] = (string) $attrNode->value;
}
If its a leaf node, store the value in @value instead of directly storing it.
if (!is_array ($output)) {
$output = Array (' @value ' = = $output);
}
$output [' @attributes '] = $a;
}
Break
}
return $output;
}

/*
* Get The root XML node, if there isn ' t one, create it.
*/
private static function Getxmlroot () {
if (Empty (self:: $xml)) {
Self::init ();
}
Return self:: $xml;
}
}
?>

Convert XML to PHP array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.