xmlParent: Get parent node of XMLInternalNode or ancestor nodes

xmlParentR Documentation

Get parent node of XMLInternalNode or ancestor nodes


xmlParent operates on an XML node and returns a reference to its parent node within the document tree. This works for an internal, C-level XMLInternalNode object created, for examply, using newXMLNode and related functions or xmlTree or from xmlTreeParse with the useInternalNodes parameter.

It is possible to find the parent of an R-level XML node when using a tree created with, for example, xmlHashTree as the parent information is stored separately.

xmlAncestors walks the chain of parens to the top of the document and either returns a list of those nodes, or alternatively a list of the values obtained by applying a function to each of the nodes.


xmlParent(x, ...)
xmlAncestors(x, fun = NULL, ..., addFinalizer = NA, count = -1L)



an object of class XMLInternalNode whose parent is being requested.


an R function which is invoked for each node as we walk up the tree.


any additional arguments that are passed in calls to fun after the node object and for xmlParent this allows methods to define their own additional parameters.


a logical value indicating whether the default finalizer routine should be registered to free the internal xmlDoc when R no longer has a reference to this external pointer object. This can also be the name of a C routine or a reference to a C routine retrieved using getNativeSymbolInfo.


an integer that indicates how many levels of the hierarchy to traverse. This allows us to get the count most recent ancestors of the node.


This uses the internal libxml structures to access the parent in the DOM tree. This function is generic so that we can add methods for other types of nodes if we so want in the future.


xmlParent returns object of class XMLInternalNode.

If fun is NULL, xmlAncestors returns a list of the nodes in order of top-most node or root of the tree, then its child, then the child of that child, etc. This is the reverse order in which the nodes are visited/found.

If fun is a function, xmlAncestors returns a list whose elements are the results of calling that function for each node. Again, the order is top down.


Duncan Temple Lang



See Also

xmlChildren xmlTreeParse xmlNode


  top = newXMLNode("doc")
  s = newXMLNode("section", attr = c(title = "Introduction"))
  a = newXMLNode("article", s)
  addChildren(top, a)


    # Find the root node.
  root = a
      root = xmlParent(root)

   # find the names of the parent nodes of each 'h' node.
   # use a global variable to "simplify" things and not use a closure.

  filename = system.file("exampleData", "branch.xml", package = "XML")
  parentNames <- character()
                handlers =
                  list(h = function(x) {
                   parentNames <<- c(parentNames, xmlName(xmlParent(x)))


XML documentation built on March 31, 2023, 6:47 p.m.

Related to xmlParent in XML...