开发者

query Using XPATH and then append child to it

开发者 https://www.devze.com 2023-01-02 00:18 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号