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