docNode = new DOMDocument(); $rc = $this->docNode->loadxml($input); if ($rc === false) throw new RuntimeException("Could not parse XML document."); $this->currentNode = $this->docNode; } public function toTop(){ $this->currentNode = $this->docNode; } public function toParent(){ $this->currentNode = $this->currentNode->parentNode; } public function extractElement($extr_ctl, &$elem, &$value, &$attrs) { $elem = ''; $value = null; $attrs = array(); if ($extr_ctl == self::EC_BEG) { $srh_node = $this->currentNode->firstChild; } elseif ($extr_ctl == self::EC_CTN) { $srh_node = $this->currentNode->nextSibling; } else { return self::EC_END; } // search for next element node while ($srh_node !== null) { if ($srh_node->nodeType !== XML_ELEMENT_NODE) { $srh_node = $srh_node->nextSibling; } else { break; } } if ($srh_node !== null) { // element node found: extract tag name, value and attributes $this->currentNode = $srh_node; $this->getCurrentNodeData($elem, $value, $attrs); return self::EC_CTN; } else { // terminate / ascend subtree after child extraction if ($extr_ctl == self::EC_CTN) { $this->currentNode = $this->currentNode->parentNode; } return self::EC_END; } } function getCurrentNodeData(&$elem, &$value, &$attrs) { $elem = $this->currentNode->tagName; if (isset($this->currentNode->nodeValue)) { $value = $this->currentNode->nodeValue; } for ($att_idx = 0; $att_idx < $this->currentNode->attributes->length; $att_idx++) { $att_node = $this->currentNode->attributes->item($att_idx); $attrs[$att_node->name] = $att_node->value; } } function gotoChild($name) { # return True/False, if child node could be found $cntl = InputExtractor::EC_BEG; while (($cntl = $instanceExtractor->extractElement($cntl, $elem, $value, $attrs)) != InputExtractor::EC_END) { if ($elem == $name) return True; } return False; } function requireChild($name) { # throws exception, if child not present if (! $this->gotoChild($name)) throw Exception("Could not find node ".$name); } }