Learn R Programming

XML (version 1.7-2)

xmlParent: Get parent node of XMLInternalNode

Description

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

Usage

xmlParent(x)

Arguments

x
an object of class XMLInternalNode whose parent is being requested.

Value

  • An object of class XMLInternalNode.

Details

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.

References

http://www.w3.org/XML

See Also

xmlChildren xmlTreeParse xmlNode

Examples

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

  xmlName(xmlParent(s))
  xmlName(xmlParent(xmlParent(s)))


    # Find the root node.
  root = a
  while(!is.null(xmlParent(root)))
      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()
  xmlTreeParse(filename,
                handlers =
                  list(h = function(x) {
                   parentNames <<- c(parentNames, xmlName(xmlParent(x)))
                  }), useInternalNodes = TRUE)

  table(parentNames)

Run the code above in your browser using DataLab