Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine Parent Node Of DOMElement

Tags:

dom

php

xml

I'm translating my C# code for YouTube video comments into PHP. In order to properly nest comment replies, I need to re-arrange XML nodes. In PHP I'm using DOMDocument and DOMXPath which closely corresponds to C# XmlDocument. I've gotten pretty far in my translation but now I'm stuck on getting the parent node of a DOMElement. A DOMElement does not have a parent_node() property, only a DOMNode provides that property.

After determining that a comment is a reply to a previous comment based in the string "in-reply-to" in a link element, I need to get its parent node in order to nest it beneath the comment it is in reply to:

// Get the parent entry node of this link element
$importnode = $objReplyXML->importNode($link->parent_node(), true);
like image 953
user2601 Avatar asked Aug 26 '08 19:08

user2601


1 Answers

DOMElement is a subclass of DOMNode, so it does have parent_node property. Just use $domNode->parentNode; to find the parent node.

In your example, the parent node of $importnode is null, because it has been imported into the document, and therefore does not have a parent yet. You need to attach it to another element before it has a parent.

like image 165
Marius Avatar answered Oct 21 '22 05:10

Marius