xmldoc: creates an 'XmlDoc' object represenging an xml file.

Description Usage Arguments Examples

Description

an 'XmlDoc' object enables access to the text and attributes of xml documents. it is not much more than syntactical sugar on top of the XML library, but it allows you forget about closing the xml document and makes accessing groups of elements a lot easier.

besides the constructor $new, there are two exposed methods, getAttribute and getText. both return character values, if you want to convert data to numeric, you have to invoke as.numeric yourself.

both getAttribute and getText first build a XMLNodeSet object based on the element and ... arguments. getAttribute also expects an attr argument, which specifies the desired attribute or attributes.

both getAttribute and getText return a character vector. only getAttribute may return a data.frame, if more than one attribute is requested.

Usage

1
2
3
4
5
6
  ## S4 method for signature 'XmlDoc'
new(fileName)
  ## S4 method for signature 'XmlDoc'
getText(element, ..., children)
  ## S4 method for signature 'XmlDoc'
getAttribute(attr, element, ...)

Arguments

fileName

the file containing the xml code.

element

the path to the element. may contain directives for filtering on attribute values.

...

the optional attribute values for the filtering directives.

attr

the attribute or attributes to select.

children

character vector: the relative paths of the names of the elements whose text to return. if specified, the return value is a matrix instead of a vector. it is mostly useful if the contained elements are unique: sequences are pasted without glue.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
  tempFileName <- paste(tempdir(), 'test.xml', sep='/')
  cat('<root><element><sub id="1" ktb="7" qlm="a">7</sub>
    <sub id="2" ktb="23" qlm="b">23</sub></element></root>',
    file=file.path(tempFileName))

  doc <- XmlDoc$new(tempFileName)
  current <- doc$getAttribute("id", "/root/element/sub")
  stopifnot(c("1","2") == current)

  current <- doc$getText("/root/element/sub")
  stopifnot(c("7", "23") == current)

  current <- doc$getText("/root/element/sub[@id=%d]", 2)
  stopifnot(c("23") == current)

  current <- doc$getAttribute(c("id", "ktb", "qlm"), "/root/element/sub")
  stopifnot(cbind(id=c("1", "2"), ktb=c("7", "23"), qlm=c("a", "b")) == current)

  doc$getText("/root/element", children="sub")  # returns a list

  file.remove(tempFileName)

delftfews documentation built on May 2, 2019, 4:48 p.m.