function xml2obj($domElement, $ident = "-") { $ret_obj = new stdClass(); if ($domElement->childNodes) { $ridx = 0; foreach ($domElement->childNodes as $c_elem) { $_t = $c_elem->tagName; if ($c_elem->nodeType == XML_ELEMENT_NODE && $c_elem->childNodes->length == 0) { $ret_obj->{$_t} = null; } elseif ($c_elem->nodeType == XML_ELEMENT_NODE && $c_elem->childNodes->length == 1 && $c_elem->childNodes->item(0)->nodeType == XML_TEXT_NODE) { $ret_obj->{$_t} = $c_elem->childNodes->item(0)->wholeText; } elseif ($c_elem->nodeType == XML_ELEMENT_NODE && $c_elem->childNodes->length >= 1) { if ($_t == 'result_set') { $n_c_elem = $c_elem; foreach ($n_c_elem->childNodes as $n) { $ret_obj->result_set[] = xml2obj($n, $ident . "-"); } } elseif ($_t == 'fields') { $n_c_elem = $c_elem; foreach ($n_c_elem->childNodes as $n) { $ret_obj->fields[] = xml2obj($n, $ident . "-"); } } else { $ret_obj->{$_t} = xml2obj($c_elem, $ident . "-"); } } } } return $ret_obj; }
/** * xml to object conversion function * @param bool $force: set to true to always create 'text', 'attribute', and 'children' even if empty * @return object with attributs: (string) name: XML tag name (string) text: text content of the attribut name (array) attributes: array witch keys are attribute key and values are attribute value (array) children: array of objects made with xml2obj() on each child */ public function xml2obj($force = false) { $obj = new StdClass(); $obj->name = $this->getName(); $text = trim((string) $this); $attributes = array(); $children = array(); foreach ($this->attributes() as $k => $v) { $attributes[$k] = (string) $v; } foreach ($this->children() as $k => $v) { $children[] = xml2obj($v, $force); } if ($force or $text !== '') { $obj->text = $text; } if ($force or count($attributes) > 0) { $obj->attributes = $attributes; } if ($force or count($children) > 0) { $obj->children = $children; } return $obj; }