예제 #1
0
 /**
  * Convert a DOM node to an intermediate nested array
  * representation that can be iterated
  * 
  * @param DOMNode $node	DOM node to convert
  */
 private static function xmlNodeToArray($node)
 {
     $result = array();
     $children = $node->childNodes;
     if (!empty($children)) {
         for ($i = 0; $i < (int) $children->length; $i++) {
             $child = $children->item($i);
             if ($child !== null) {
                 if ($child->childNodes->item(0) instanceof \DOMText) {
                     $result[$i]['name'] = $child->nodeName;
                     $result[$i]['text'] = $child->childNodes->item(0)->nodeValue;
                     if ($child->hasAttributes()) {
                         foreach ($child->attributes as $k => $v) {
                             if ($v->namespaceURI != 'http://www.w3.org/2001/XMLSchema-instance') {
                                 $result[$i]['attributes'][$v->name] = $v->value;
                             }
                         }
                     }
                 } else {
                     if (!in_array($child->nodeName, $result)) {
                         $result[$i]['name'] = $child->nodeName;
                         $result[$i]['children'] = PPUtils::xmlNodeToArray($child);
                         if ($child->hasAttributes()) {
                             $attrs = $child->attributes;
                             foreach ($attrs as $k => $v) {
                                 if ($v->namespaceURI != 'http://www.w3.org/2001/XMLSchema-instance') {
                                     $result[$i]['attributes'][$v->name] = $v->value;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return $result;
 }