@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | |
| 4 | 4 | class XML2Array { |
| 5 | 5 | private static $xml = null; |
| 6 | - private static $encoding = 'UTF-8'; |
|
| 6 | + private static $encoding = 'UTF-8'; |
|
| 7 | 7 | /** |
| 8 | 8 | * Initialize the root XML node [optional] |
| 9 | 9 | * @param $version |
@@ -13,7 +13,7 @@ discard block |
||
| 13 | 13 | public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) { |
| 14 | 14 | self::$xml = new \DOMDocument($version, $encoding); |
| 15 | 15 | self::$xml->formatOutput = $format_output; |
| 16 | - self::$encoding = $encoding; |
|
| 16 | + self::$encoding = $encoding; |
|
| 17 | 17 | } |
| 18 | 18 | /** |
| 19 | 19 | * Convert an XML to Array |
@@ -23,18 +23,18 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | public static function &createArray($input_xml) { |
| 25 | 25 | $xml = self::getXMLRoot(); |
| 26 | - if(is_string($input_xml)) { |
|
| 27 | - $parsed = $xml->loadXML($input_xml); |
|
| 28 | - if(!$parsed) { |
|
| 29 | - throw new \Exception('[XML2Array] Error parsing the XML string.'); |
|
| 30 | - } |
|
| 31 | - } else { |
|
| 32 | - if(get_class($input_xml) != 'DOMDocument') { |
|
| 33 | - throw new \Exception('[XML2Array] The input XML object should be of type: DOMDocument.'); |
|
| 34 | - } |
|
| 35 | - $xml = self::$xml = $input_xml; |
|
| 36 | - } |
|
| 37 | - $array[$xml->documentElement->tagName] = self::convert($xml->documentElement); |
|
| 26 | + if(is_string($input_xml)) { |
|
| 27 | + $parsed = $xml->loadXML($input_xml); |
|
| 28 | + if(!$parsed) { |
|
| 29 | + throw new \Exception('[XML2Array] Error parsing the XML string.'); |
|
| 30 | + } |
|
| 31 | + } else { |
|
| 32 | + if(get_class($input_xml) != 'DOMDocument') { |
|
| 33 | + throw new \Exception('[XML2Array] The input XML object should be of type: DOMDocument.'); |
|
| 34 | + } |
|
| 35 | + $xml = self::$xml = $input_xml; |
|
| 36 | + } |
|
| 37 | + $array[$xml->documentElement->tagName] = self::convert($xml->documentElement); |
|
| 38 | 38 | self::$xml = null; // clear the xml node in the class for 2nd time use. |
| 39 | 39 | return $array; |
| 40 | 40 | } |
@@ -44,60 +44,60 @@ discard block |
||
| 44 | 44 | * @return mixed |
| 45 | 45 | */ |
| 46 | 46 | private static function &convert($node) { |
| 47 | - $output = array(); |
|
| 48 | - switch ($node->nodeType) { |
|
| 49 | - case XML_CDATA_SECTION_NODE: |
|
| 50 | - $output['@cdata'] = trim($node->textContent); |
|
| 51 | - break; |
|
| 52 | - case XML_TEXT_NODE: |
|
| 53 | - $output = trim($node->textContent); |
|
| 54 | - break; |
|
| 55 | - case XML_ELEMENT_NODE: |
|
| 56 | - // for each child node, call the covert function recursively |
|
| 57 | - for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) { |
|
| 58 | - $child = $node->childNodes->item($i); |
|
| 59 | - $v = self::convert($child); |
|
| 60 | - if(isset($child->tagName)) { |
|
| 61 | - $t = $child->tagName; |
|
| 62 | - // assume more nodes of same kind are coming |
|
| 63 | - if(!isset($output[$t])) { |
|
| 64 | - $output[$t] = array(); |
|
| 65 | - } |
|
| 66 | - $output[$t][] = $v; |
|
| 67 | - } else { |
|
| 68 | - //check if it is not an empty text node |
|
| 69 | - if($v !== '') { |
|
| 70 | - $output = $v; |
|
| 71 | - } |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - if(is_array($output)) { |
|
| 75 | - // if only one node of its kind, assign it directly instead if array($value); |
|
| 76 | - foreach ($output as $t => $v) { |
|
| 77 | - if(is_array($v) && count($v)==1) { |
|
| 78 | - $output[$t] = $v[0]; |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - if(empty($output)) { |
|
| 82 | - //for empty nodes |
|
| 83 | - $output = ''; |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - // loop through the attributes and collect them |
|
| 87 | - if($node->attributes->length) { |
|
| 88 | - $a = array(); |
|
| 89 | - foreach($node->attributes as $attrName => $attrNode) { |
|
| 90 | - $a[$attrName] = (string) $attrNode->value; |
|
| 91 | - } |
|
| 92 | - // if its an leaf node, store the value in @value instead of directly storing it. |
|
| 93 | - if(!is_array($output)) { |
|
| 94 | - $output = array('@value' => $output); |
|
| 95 | - } |
|
| 96 | - $output['@attributes'] = $a; |
|
| 97 | - } |
|
| 98 | - break; |
|
| 99 | - } |
|
| 100 | - return $output; |
|
| 47 | + $output = array(); |
|
| 48 | + switch ($node->nodeType) { |
|
| 49 | + case XML_CDATA_SECTION_NODE: |
|
| 50 | + $output['@cdata'] = trim($node->textContent); |
|
| 51 | + break; |
|
| 52 | + case XML_TEXT_NODE: |
|
| 53 | + $output = trim($node->textContent); |
|
| 54 | + break; |
|
| 55 | + case XML_ELEMENT_NODE: |
|
| 56 | + // for each child node, call the covert function recursively |
|
| 57 | + for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) { |
|
| 58 | + $child = $node->childNodes->item($i); |
|
| 59 | + $v = self::convert($child); |
|
| 60 | + if(isset($child->tagName)) { |
|
| 61 | + $t = $child->tagName; |
|
| 62 | + // assume more nodes of same kind are coming |
|
| 63 | + if(!isset($output[$t])) { |
|
| 64 | + $output[$t] = array(); |
|
| 65 | + } |
|
| 66 | + $output[$t][] = $v; |
|
| 67 | + } else { |
|
| 68 | + //check if it is not an empty text node |
|
| 69 | + if($v !== '') { |
|
| 70 | + $output = $v; |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + if(is_array($output)) { |
|
| 75 | + // if only one node of its kind, assign it directly instead if array($value); |
|
| 76 | + foreach ($output as $t => $v) { |
|
| 77 | + if(is_array($v) && count($v)==1) { |
|
| 78 | + $output[$t] = $v[0]; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + if(empty($output)) { |
|
| 82 | + //for empty nodes |
|
| 83 | + $output = ''; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + // loop through the attributes and collect them |
|
| 87 | + if($node->attributes->length) { |
|
| 88 | + $a = array(); |
|
| 89 | + foreach($node->attributes as $attrName => $attrNode) { |
|
| 90 | + $a[$attrName] = (string) $attrNode->value; |
|
| 91 | + } |
|
| 92 | + // if its an leaf node, store the value in @value instead of directly storing it. |
|
| 93 | + if(!is_array($output)) { |
|
| 94 | + $output = array('@value' => $output); |
|
| 95 | + } |
|
| 96 | + $output['@attributes'] = $a; |
|
| 97 | + } |
|
| 98 | + break; |
|
| 99 | + } |
|
| 100 | + return $output; |
|
| 101 | 101 | } |
| 102 | 102 | /* |
| 103 | 103 | * Get the root XML node, if there isn't one, create it. |