xml_to_list: Converting XML documents to lists

Description Usage Arguments Value Examples

View source: R/xml_convert.r

Description

Converts XML documents to lists. Uses the as_list() function from the xml2 package but improves its output. As an effect, numbers, dates and times are converted correctly, unnecessary nested sub-lists with only element are avoided, and empty XML nodes can be removed altogether. This makes the resulting list look cleaner and better structured.

Usage

1
2
3
4
5
6
7
8
9
xml_to_list(
  xml,
  cleanup = TRUE,
  convert.types = TRUE,
  dec = ".",
  thsd = ",",
  num.replace = "",
  datetime.formats = NULL
)

Arguments

xml

XML document to be converted. Can be read in from a file using xml2's read_xml() function.

cleanup

If TRUE (default) empty XML nodes (with no sub-nodes or values) will not appear in the list.

convert.types

If TRUE (default) xml_to_list() will try to infer the data type of value elements in the XML. If FALSE, all value elements in the resulting list will be of type character.

dec

Decimal separator used in numbers.

thsd

Thousands separator used in numbers.

num.replace

An optional string that will be removed before xml_to_list() tries to convert values to numbers. Can be used, for example, to remove currency symbols or other measurement units from values that are actually numerical.

datetime.formats

A vector of date and/or time formats that will be used to recognize the respective datatypes. Formats will need to be written in the general notation used by strftime() and other standard R functions.

Value

A list object representing the XML document.

Examples

1
2
xml <- xml2::read_xml(system.file("customers.xml", package="xmlconvert"))
xml.list <- xml_to_list(xml, num.replace="USD", datetime.formats = "%Y-%m-%d")#'

xmlconvert documentation built on March 27, 2021, 9:09 a.m.