Skip to contents

add an element to an xmlbuilder object

Usage

elem(tag, text = NULL, ...)

Arguments

tag

name of element

text

text contents of element

...

additional xml. attributes to be set

Value

an xmlbuilder object

Examples

xb <- elem("homeless") /
  elem("person") / (
     elem("name","John Doe") +
     elem("age",35)
  ) +
  elem("person") /(
    elem("name","Jane Doe") +
    elem("age", 30)
  ) +
  elem("person") / (
    elem("name","Jim Doe") +
    elem("age", 25) +
    elem("address") / (
      elem("street", "123 Main St") +
      elem("city", "Anytown") +
      elem("state", "CA") +
      elem("zip", 12345)
    )
  )

print(xb)
#> {xmlbuilder}
#> <homeless><person><name>John Doe</name><age>35</age><person><name>Jane Doe</name><age>30</age><person><name>Jim Doe</name><age>25</age><address><street>123 Main St</street><city>Anytown</city><state>CA</state><zip>12345
xb$end()
xb$end()


doc <- xb |> xml2::as_xml_document()
doc |> as.character() |> cat()
#> <?xml version="1.0" encoding="UTF-8"?>
#> <homeless>
#>   <person>
#>     <name>John Doe</name>
#>     <age>35</age>
#>     <person>
#>       <name>Jane Doe</name>
#>       <age>30</age>
#>       <person>
#>         <name>Jim Doe</name>
#>         <age>25</age>
#>         <address>
#>           <street>123 Main St</street>
#>           <city>Anytown</city>
#>           <state>CA</state>
#>           <zip>12345</zip>
#>         </address>
#>       </person>
#>     </person>
#>   </person>
#> </homeless>