Skip to contents

list_as_xml_document is fast and efficient way to convert a list to an xml2::xml_document. The preferred interface is to use xml_fragment() and xml_doc() to create xml fragments.

Usage

list_as_xml_document(x, ...)

Arguments

x

a list as returned by xml2::as_list()

...

reserved for future use

Value

an xml2::xml_document

Details

list_to_xml_document is a much faster implementation of xml2::as_xml_document.list() method. It writes the xml directly to a string buffer and then reads it back into an xml2::xml_document.

The function can be used in tandem with xml2::as_list() to convert R data structures.

See also

Examples

data <-
  list(
    study = list(
      person = list(
        name = "John Doe",
        age = "30"
      ),
      person = list(
        name = "Jane Doe",
        age = "25"
      )
    )
  )

list_as_xml_string(data)
#> [1] "<study>\n  <person>\n    <name>John Doe</name>\n    <age>30</age>\n  </person>\n  <person>\n    <name>Jane Doe</name>\n    <age>25</age>\n  </person>\n</study>"
if (require("xml2")){
  list_as_xml_document(data)
}
#> {xml_document}
#> <study>
#> [1] <person>\n  <name>John Doe</name>\n  <age>30</age>\n</person>
#> [2] <person>\n  <name>Jane Doe</name>\n  <age>25</age>\n</person>

#note the xml_fragment function is more powerful to create lists

data <- xml_doc("study", id = "1") /
  frag(
    person = frag(
      name = "John Doe",
      age = "30"
    ),
    person = frag(
      name = "Jane Doe",
      age = "25"
    ),
    "This is a text node"
)

list_as_xml_string(data)
#> [1] "<study id=\"1\">\n  <person>\n    <name>John Doe</name>\n    <age>30</age>\n  </person>\n  <person>\n    <name>Jane Doe</name>\n    <age>25</age>\n  </person>This is a text node\n</study>"