query Using XPATH and then append child to it
I have a xml file, and i need to append a child to it if a parent exists. So i used xpath 开发者_如何学运维to query for that specific node.
$dom = new DomDocument();
$dom->load('testing.xml');
$xp = new domxpath($dom);
$category = $xp->query("tree[@heading='something']");
Now i am not sure how to append a child to this result. The variable $category is a object when i do print_r($category).
Thanks
$category
will be a DOMNodeList
, so to access the matching tree
elements you can either iterate over them with a loop (e.g. foreach ($category as $tree) { ... }
) or access them by index (e.g. $tree = $category->item(0)
is the first matching tree
).
In each case, $tree
will be a DOMElement
which has the appendChild
method which you can use to append the child.
精彩评论