xmlElementsByTagName {XML} | R Documentation

## Retrieve the children of an XML node with a specific tag name

### Description

This returns a list of the children or sub-elements of an XML node whose tag name matches the one specified by the user.

### Usage

    xmlElementsByTagName(el, name, recursive = FALSE)

### Arguments

`el` | the node whose matching children are to be retrieved.
`name` | a string giving the name of the tag to match in each of `el`'s children.

`recursive`

a logical value. If this is `FALSE`, the default, only the direct child nodes are searched. Alternatively, if this is `TRUE`, all sub-nodes at all levels are searched. In other words, we find all descendants of the node `el` and return a list with the nodes having the given name. The relationship between the nodes in the resulting list cannot be determined. This is a set of nodes. See the note.

### Details

This does a simple matching of names and subsets the XML node's children list. If `recursive` is `TRUE`, then the function is applied recursively to the children of the given node and so on.

### Value

A list containing those child nodes of `el` whose tag name matches that specified by the user.

### Note

The addition of the `recursive` argument makes this function behave like the `getElementsByTagName` in other language APIs such as Java, C\#. However, one should be careful to understand that in those languages, one would get back a set of node objects. These nodes have references to their parents and children. Therefore one can navigate the tree from each node, find its relations, etc. In the current version of this package (and for the forseeable future), the node set is a “copy” of the nodes in the original tree. And these have no facilities for finding their siblings or parent. Additionally, one can consume a large amount of memory by taking a copy of numerous large nodes using this facility. If one does not modify the nodes, the extra memory may be small. But modifying them means that the contents will be copied.

Alternative implementations of the tree, e.g. using unique identifiers for nodes or via internal data structures from libxml can allow us to implement this function with different semantics, more similar to the other APIs.

### Author(s)

Duncan Temple Lang

### References

http://www.w3.org/XML, http://www.omegahat.net/RSXML,

### See Also

`xmlChildren` `xmlTreeParse`

### Examples

    ## Not run:
     doc <- xmlTreeParse("http://www.omegahat.net/Scripts/Data/mtcars.xml")
     xmlElementsByTagName(doc$children[[1]], "variable")

    ## End(Not run)

     doc <- xmlTreeParse(system.file("exampleData", "mtcars.xml", package="XML"))
     xmlElementsByTagName(xmlRoot(doc)[[1]], "variable")

[Package XML version 3.98-1.5 Index]