确定DOMElement的父节点

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);

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.

I'm not entirely sure how your code works, but it seems like you have a small error in your code.

On the line you posted in your question you have $link->parent_node(), but in the answer with the entire code snippet you have $link**s**->parent_node().

I don't think the s should be there.

Also, I think you should use $link->parentNode, not $link->parent_node().

Replace parent_node() to parentNode