tags:

views:

14

answers:

1

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

+1  A: 

$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.

salathe
Thanks a lot...that worked for me... :)
noobcode