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
$categorywill be aDOMNodeList, so to access the matchingtreeelements 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 matchingtree).In each case,
$treewill be aDOMElementwhich has theappendChildmethod which you can use to append the child.