function bench2() { global $XML; $dom = new domDocument(); $dom->loadxml($XML); $children = $dom->getElementsByTagName('title'); foreach ($children as $v) { $arr[] = $v->nodeValue; } return $arr; }
function bench2() { global $XML; $dom = new domDocument(); $dom->loadxml($XML); $xp = new domxpath($dom); $nodes = $xp->query("//title/text()"); foreach ($nodes as $n) { $arr[] = $n->nodeValue; } return $arr; /* // simplexml syntax... $s = simplexml_load_string($XML); $nodes = $s->xpath("//title"); foreach($nodes as $v) { $arr[] = reset($v); } return $arr; */ }
<?php require_once "dom1.inc"; echo "Test 1: accessing single nodes from php\n"; $dom = new domDocument(); $dom->loadxml($xmlstr); if (!$dom) { echo "Error while parsing the document\n"; exit; } // children() of of document would result in a memleak //$children = $dom->children(); //print_node_list($children); echo "--------- root\n"; $rootnode = $dom->documentElement; print_node($rootnode); echo "--------- children of root\n"; $children = $rootnode->childNodes; print_node_list($children); // The last node should be identical with the last entry in the children array echo "--------- last\n"; $last = $rootnode->lastChild; print_node($last); // The parent of this last node is the root again echo "--------- parent\n"; $parent = $last->parentNode; print_node($parent); // The children of this parent are the same children as one above echo "--------- children of parent\n"; $children = $parent->childNodes; print_node_list($children);
<?php $xml = '<?xml version="1.0"?><dependencies><dependency dependency_id="0" dependent_id="1"/><dependency dependency_id="4" dependent_id="5"/><dependency dependency_id="5" dependent_id="6"/><dependency dependency_id="9" dependent_id="8"/><dependency dependency_id="10" dependent_id="8"/><dependency dependency_id="12" dependent_id="13"/><dependency dependency_id="12" dependent_id="14"/></dependencies>'; $dom = new domDocument(); $dom->loadxml($xml); $xpath = new DOMXPath($dom); $node_list = $xpath->query('//dependencies/dependency[@dependency_id = 0 and @dependent_id = 1]'); $dependencies = $xpath->query('//dependencies')->item(0); $dependencies->removeChild($node_list->item(0));