xml: Generic XML creation

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/xml.R

Description

xml is a S3-generic function that builds xml objects

Usage

1
2
3
4
5
6
7
8
9
xml(x, ..., env = sys.frame(sys.parent()))
## S3 method for class 'XMLNode'
x[i  , verbose = TRUE, drop = getOption("R4X.drop") ]
## S3 method for class 'XMLNode'
x[[i]]
## S3 replacement method for class 'XMLNode'
x[i] <- value
## S3 replacement method for class 'XMLNode'
x[[i]] <- value

Arguments

x

Object of which to build an xml representation

...

Additional arguments potentially used in methods

env

Environment in which to work. It is set by default to the environment from which the function is called.

i

index of extraction

value

replacement

verbose

Print additional information

drop

Used with the single extractor. When TRUE and the result of the extract is a single node, it is returned as an XMLNode object, otherwise as a list of one XMLNode. The drop argument is set by the option R4X.drop which is set to TRUE when the R4X package is attached.

Details

Distilling as defined in distill can be used to build the xml object.

The extractors for XMLNode objects as defined here use a pseudo-xpath syntax to define which part of the XML structure is extracted. For full xpath capabilities, see the xpathApply function.

The string inside the square bracket is divided into several tokens separated by a forward slash. Each token allows to go down on the XML structure from the top level. Unlike xpath, there is no need for the initial slash is meaningless and will be ignored.

Tokens have the following meaning:

Description use
Standard character string name of the child XML tag
Character string starting with @ attribute
Character string starting with \~ Regular expression to identify the tags
Character string containing square bracket Extract given tags and subset them
Hash symbol Content of the node

Value

An object of class XMLNode from the XML package is returned.

Author(s)

Romain Francois <rfrancois@mango-solutions.com>

See Also

distill

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
         
sales <- xml( 
  '<sales vendor="John">
      <item type="peas" price="4" quantity="6"/>
      <item type="carrot" price="3" quantity="10"/>
      <item type="chips" price="5" quantity="3"/>
  </sales>' )  
      
### example of distilling, see ?distill for details 
y <- "peas" 
sales <- xml( 
  '<sales vendor="John">
  <item type="{y}"/>
  </sales>' )  
sales

y <- c( "peas", "carrot", "chips" ) 
sales <- xml( 
'<sales vendor="John">
  <@j|length(y)>
    <item type="{y[j]}"/>
  </@>  
</sales>' )  
sales

y <- c( "peas", "carrot", "chips" ) 
sales <- xml( 
'<sales vendor="John">
  <@item~y>
    <item type="{item}"/>
  </@>  
</sales>' )  
sales

y <- c( "peas", "carrot", "chips" ) 
sales <- xml( 
'<sales vendor="John">
  <@index?y>
    <item type="{y[index]}"/>
  </@>  
</sales>' )  
sales

### examples of extraction
x <- xml( '<root>
  <child id="1">
    <subchild id = "sub1" >foo</subchild>
    <subchild id = "sub2" >bar</subchild>
  </child>
  <child id="2">
    <subchild id="a">blah</subchild>
    <subchild id="b">bob</subchild>
    <something id="c" />
  </child>
  <fruits>                     
     <fruit>banana</fruit>
     <fruit>mango</fruit>
  </fruits>
</root>' )
x[ 'fruits/fruit' ]            # list of two XMLNode
x[ 'fruits/fruit/#' ]          # c("banana", "mango" )
x[ 'fruits' ]                  # one XMLNode, example with drop set to TRUE
x[ 'fruits', drop = TRUE  ]    # same, but explicit
x[ 'fruits', drop = FALSE ]    # same, but the result is returned as a list of one XMLNode
x[ 'child/subchild' ]          # list of 4 XMLNode
x[ 'child/subchild/@id' ]      # vector of id attributes
x[ 'child/~^s' ]               # list of 5 XMLNode
x[ 'child[1]/subchild' ]       # list of 2 XMLNode
x[ 'child[1]/subchild[1]/#' ]  # "foo"
x[ 'child[1]/1/@id' ]          # "sub1"

R4X documentation built on May 2, 2019, 4:52 p.m.