Tutorial Extension  1.0.0
SellerDeck Extensions - Tutorial Extension
XML2Array.php
1 <?php
2 
3 /**
4  * XML2Array: A class to convert XML to array in PHP
5  * It returns the array which can be converted back to XML using the Array2XML script
6  * It takes an XML string or a DOMDocument object as an input.
7  *
8  * See Array2XML: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes
9  *
10  * Author : Lalit Patel
11  * Website: http://www.lalit.org/lab/convert-xml-to-array-in-php-xml2array
12  * License: Apache License 2.0
13  * http://www.apache.org/licenses/LICENSE-2.0
14  * Version: 0.1 (07 Dec 2011)
15  * Version: 0.2 (04 Mar 2012)
16  * Fixed typo 'DomDocument' to 'DOMDocument'
17  *
18  * Usage:
19  * $array = XML2Array::createArray($xml);
20  */
21 
22 namespace SDExtension\Helper;
23 
24 class XML2Array
25  {
26 
27  private static $xml = null;
28  private static $encoding = 'UTF-8';
29 
30  /**
31  * Initialize the root XML node [optional]
32  * @param $version
33  * @param $encoding
34  * @param $format_output
35  */
36  public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true)
37  {
38  self::$xml = new \DOMDocument($version, $encoding);
39  self::$xml->formatOutput = $format_output;
40  self::$encoding = $encoding;
41  }
42 
43  /**
44  * Convert an XML to Array
45  * @param string $node_name - name of the root node to be converted
46  * @param array $arr - aray to be converterd
47  * @return DOMDocument
48  */
49  public static function &createArray($input_xml)
50  {
51  $xml = self::getXMLRoot();
52  if (is_string($input_xml))
53  {
54  $parsed = $xml->loadXML($input_xml);
55  if (!$parsed)
56  {
57  throw new \Exception('[XML2Array] Error parsing the XML string.');
58  }
59  }
60  else
61  {
62  if (get_class($input_xml) != 'DOMDocument')
63  {
64  throw new \Exception('[XML2Array] The input XML object should be of type: DOMDocument.');
65  }
66  $xml = self::$xml = $input_xml;
67  }
68  $array[$xml->documentElement->tagName] = self::convert($xml->documentElement);
69  self::$xml = null; // clear the xml node in the class for 2nd time use.
70  return $array;
71  }
72 
73  /**
74  * Convert an Array to XML
75  * @param mixed $node - XML as a string or as an object of DOMDocument
76  * @return mixed
77  */
78  private static function &convert($node)
79  {
80  $output = array();
81 
82  switch ($node->nodeType)
83  {
84  case XML_CDATA_SECTION_NODE:
85  $output['@cdata'] = trim($node->textContent);
86  break;
87 
88  case XML_TEXT_NODE:
89  $output = trim($node->textContent);
90  break;
91 
92  case XML_ELEMENT_NODE:
93  $node->namespaceURI;
94 // for each child node, call the covert function recursively
95  for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++)
96  {
97  $child = $node->childNodes->item($i);
98  $v = self::convert($child);
99  if (isset($child->tagName))
100  {
101  $t = $child->tagName;
102 
103 // assume more nodes of same kind are coming
104  if (!isset($output[$t]))
105  {
106  $output[$t] = array();
107  }
108  $output[$t][] = $v;
109  }
110  else
111  {
112 //check if it is not an empty text node
113  if ($v !== '')
114  {
115  $output = $v;
116  }
117  }
118  }
119 
120  if (is_array($output))
121  {
122 // if only one node of its kind, assign it directly instead if array($value);
123  foreach ($output as $t => $v)
124  {
125  if (is_array($v) && count($v) == 1)
126  {
127  $output[$t] = $v[0];
128  }
129  }
130  if (empty($output))
131  {
132 //for empty nodes
133  $output = '';
134  }
135  }
136 
137 // loop through the attributes and collect them
138  if ($node->attributes->length)
139  {
140  $a = array();
141  foreach ($node->attributes as $attrName => $attrNode)
142  {
143  $a[$attrName] = (string) $attrNode->value;
144  }
145 // if its an leaf node, store the value in @value instead of directly storing it.
146  if (!is_array($output))
147  {
148  $output = array('@value' => $output);
149  }
150  $output['@attributes'] = $a;
151  }
152  break;
153  }
154  return $output;
155  }
156 
157  /*
158  * Get the root XML node, if there isn't one, create it.
159  */
160 
161  private static function getXMLRoot()
162  {
163  if (empty(self::$xml))
164  {
165  self::init();
166  }
167  return self::$xml;
168  }
169 
170  }
static init($version= '1.0', $encoding= 'UTF-8', $format_output=true)
Definition: XML2Array.php:36
static & createArray($input_xml)
Definition: XML2Array.php:49