Guest User

Untitled

a guest
Aug 31st, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.85 KB | None | 0 0
  1. <?php
  2.  
  3. function xmlToArray($xml, $options = array()) {
  4.        
  5.     $defaults = array(
  6.         'namespaceSeparator' => ':',//you may want this to be something other than a colon
  7.         'attributePrefix' => '@',   //to distinguish between attributes and nodes with the same name
  8.         'alwaysArray' => array(),   //array of xml tag names which should always become arrays
  9.         'autoArray' => true,        //only create arrays for tags which appear more than once
  10.         'textContent' => '$',       //key used for the text content of elements
  11.         'autoText' => true,         //skip textContent key if node has no attributes or child nodes
  12.         'keySearch' => false,       //optional search and replace on tag and attribute names
  13.         'keyReplace' => false       //replace values for above search values (as passed to str_replace())
  14.     );
  15.     $options = array_merge($defaults, $options);
  16.     $namespaces = $xml->getDocNamespaces();
  17.     $namespaces[''] = null; //add base (empty) namespace
  18.  
  19.     //get attributes from all namespaces
  20.     $attributesArray = array();
  21.     foreach ($namespaces as $prefix => $namespace) {
  22.         foreach ($xml->attributes($namespace) as $attributeName => $attribute) {
  23.             //replace characters in attribute name
  24.             if ($options['keySearch']) $attributeName =
  25.                     str_replace($options['keySearch'], $options['keyReplace'], $attributeName);
  26.             $attributeKey = $options['attributePrefix']
  27.                     . ($prefix ? $prefix . $options['namespaceSeparator'] : '')
  28.                     . $attributeName;
  29.             $attributesArray[$attributeKey] = (string)$attribute;
  30.         }
  31.     }
  32.  
  33.     //get child nodes from all namespaces
  34.     $tagsArray = array();
  35.     foreach ($namespaces as $prefix => $namespace) {
  36.         foreach ($xml->children($namespace) as $childXml) {
  37.             //recurse into child nodes
  38.             $childArray = $this->xmlToArray($childXml, $options);
  39.             list($childTagName, $childProperties) = each($childArray);
  40.  
  41.             //replace characters in tag name
  42.             if ($options['keySearch']) $childTagName =
  43.                     str_replace($options['keySearch'], $options['keyReplace'], $childTagName);
  44.             //add namespace prefix, if any
  45.             if ($prefix) $childTagName = $prefix . $options['namespaceSeparator'] . $childTagName;
  46.  
  47.             if (!isset($tagsArray[$childTagName])) {
  48.                 //only entry with this key
  49.                 //test if tags of this type should always be arrays, no matter the element count
  50.                 $tagsArray[$childTagName] =
  51.                         in_array($childTagName, $options['alwaysArray']) || !$options['autoArray']
  52.                         ? array($childProperties) : $childProperties;
  53.             } elseif (
  54.                 is_array($tagsArray[$childTagName]) && array_keys($tagsArray[$childTagName])
  55.                 === range(0, count($tagsArray[$childTagName]) - 1)
  56.             ) {
  57.                 //key already exists and is integer indexed array
  58.                 $tagsArray[$childTagName][] = $childProperties;
  59.             } else {
  60.                 //key exists so convert to integer indexed array with previous value in position 0
  61.                 $tagsArray[$childTagName] = array($tagsArray[$childTagName], $childProperties);
  62.             }
  63.         }
  64.     }
  65.  
  66.     //get text content of node
  67.     $textContentArray = array();
  68.     $plainText = trim((string)$xml);
  69.     if ($plainText !== '') $textContentArray[$options['textContent']] = $plainText;
  70.  
  71.     //stick it all together
  72.     $propertiesArray = !$options['autoText'] || $attributesArray || $tagsArray || ($plainText === '')
  73.             ? array_merge($attributesArray, $tagsArray, $textContentArray) : $plainText;
  74.  
  75.     //return node as array
  76.     return array(
  77.         $xml->getName() => $propertiesArray
  78.     );
  79. }
Add Comment
Please, Sign In to add comment