Learn R Programming

XML (version 0.7-1)

xmlDOMApply: Apply function to nodes in an XML tree/DOM.

Description

This recursively applies the specified function to each node in an XML tree and then to each of the children nodes. The result is returned as a tree and additionally entire nodes can be dropped.

Usage

xmlDOMApply(dom, func)

Arguments

dom
a node in the XML tree or DOM on which to recursively apply the given function. This should not be the XMLDocument itself returned from xmlTreeParse but an object of class XMLNode
func
the function to be applied to each node in the XML tree. This is passed the node object for the and the return value is inserted into the new tree that is to be returned in the corresponding position as the node being processed. If the return value is

Value

  • A tree that parallels the structure in the dom object passed to it.

Details

This is a native (C code) implementation that understands the structure of an XML DOM returned from xmlTreeParse and iterates over the nodes in that tree.

References

http://www.w3.org/XML, http://www.jclark.com/xml, http://www.omegahat.org

See Also

xmlTreeParse

Examples

Run this code
dom <- xmlTreeParse(system.file("exampleData","mtcars.xml", package="XML"))
 tagNames <- function() {
    tags <- character(0)
    add <- function(x) {
      if(inherits(x, "XMLNode")) {
        if(is.na(match(xmlName(x), tags)))
           tags <<- c(tags, xmlName(x))
      }

      NULL
    }

    return(list(add=add, tagNames = function() {return(tags)}))
 }

 h <- tagNames()
 xmlDOMApply(xmlRoot(dom), h$add) 
 h$tagNames()

Run the code above in your browser using DataLab