Example #1
0
 public function toArray($xml, $firstCall = true)
 {
     if (is_string($xml)) {
         $xml = new SimpleXMLElement($xml);
     }
     $children = $xml->children();
     if (!$children) {
         $r = (string) $xml;
         if ($r == 'true' || $r == 'false') {
             $r = $r == 'true';
         }
         return $r;
     }
     $arr = array();
     if ($firstCall) {
         XmlFormat::$attribute_names = array();
         XmlFormat::$root_name = $xml->getName();
         if (XmlFormat::$parse_namespaces) {
             foreach ($xml->getDocNamespaces(TRUE) as $namepace => $uri) {
                 $arr[$namepace == '' ? 'xmlns' : 'xmlns:' . $namepace] = (string) $uri;
             }
         }
     }
     if (XmlFormat::$parse_attributes) {
         foreach ($xml->attributes() as $attName => $attValue) {
             $arr[$attName] = (string) $attValue;
             XmlFormat::$attribute_names[] = $attName;
         }
     }
     foreach ($children as $key => $node) {
         $node = $this->toArray($node, false);
         if ($key == 'anon') {
             $key = count($arr);
         }
         if (isset($arr[$key])) {
             if (!is_array($arr[$key]) || @$arr[$key][0] == null) {
                 $arr[$key] = array($arr[$key]);
             }
             $arr[$key][] = $node;
         } else {
             $arr[$key] = $node;
         }
     }
     return $arr;
 }
Example #2
0
 /**
  * Convert an XML document to a multi dimensional array
  * Pass in an XML document (or SimpleXMLElement object) and this
  * recrusively loops through and builds a representative array
  *
  * @param string $xml - XML document - can optionally be a
  * SimpleXMLElement object
  * @return array ARRAY
  * @link http://bit.ly/n85yLi
  */
 public function toArray($xml, $firstCall = true)
 {
     if (is_string($xml)) {
         $xml = new SimpleXMLElement($xml);
     }
     $children = $xml->children();
     if (!$children) {
         $r = (string) $xml;
         if ($r == 'true' || $r == 'false') {
             $r = $r == 'true';
         }
         return $r;
     }
     $arr = array();
     if ($firstCall) {
         //reset the attribute names list
         self::$attribute_names = array();
         self::$root_name = $xml->getName();
         if (self::$parse_namespaces) {
             foreach ($xml->getDocNamespaces(TRUE) as $namepace => $uri) {
                 $arr[$namepace == '' ? 'xmlns' : 'xmlns:' . $namepace] = (string) $uri;
             }
         }
     }
     if (self::$parse_attributes) {
         foreach ($xml->attributes() as $attName => $attValue) {
             $arr[$attName] = (string) $attValue;
             //add to attribute list for round trip support
             self::$attribute_names[] = $attName;
         }
     }
     foreach ($children as $key => $node) {
         $node = $this->toArray($node, false);
         // support for 'anon' non-associative arrays
         if ($key == 'anon') {
             $key = count($arr);
         }
         // if the node is already set, put it into an array
         if (isset($arr[$key])) {
             if (!is_array($arr[$key]) || @$arr[$key][0] == null) {
                 $arr[$key] = array($arr[$key]);
             }
             $arr[$key][] = $node;
         } else {
             $arr[$key] = $node;
         }
     }
     return $arr;
 }