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()
xmlParse(filename,
handlers =
list(h = function(x) {
parentNames <<- c(parentNames, xmlName(xmlParent(x)))
}))
table(parentNames)
Run the code above in your browser using DataCamp Workspace