Home > OS >  xml2: How to convert an xml object to string (in memory, as R object)
xml2: How to convert an xml object to string (in memory, as R object)

Time:03-07

I have the following situation:

  • I parse an xml file using xml2::read_xml
  • I change some content in the xml document. The details are not relevant for the question.
  • Now I want to send the manipulated xml data to an API. Therefore I need a text represantation of that xml data. For this purpose, the XML package had the function XML::toString. I cannot find an equivalent in the xml2-Package. Therefore I do it like this:
    tmpfile <- tempfile(fileext = ".xml")
    xml2::write_xml(xml, file = tmpfile)
    txt <- readLines(tmpfile, encoding = "UTF-8")
    file.remove(tmpfile)
    call_api(txt)
    
  • I am doing this for several hundret files.

Writing to disk something that could be done in memory is very inefficient.

Am I missing an obvious function in the xml2 package?

CodePudding user response:

The xml2 package provides as.character() methods for its object classes. So, using a built-in example, you can simply do:

library(xml2)
dat <- read_xml(xml2_example("order-doc.xml"))

as.character(dat)
[1] "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<purchaseOrder xmlns=\"http://tempuri.org/po.xsd\" orderDate=\"1999-10-20\">\n  <shipTo country=\"US\">\n    <name>Alice Smith</name>\n    <street>123 Maple Street</street>\n    <city>Mill Valley</city>\n    <state>CA</state>\n    <zip>90952</zip>\n  </shipTo>\n  <billTo country=\"US\">\n    <name>Robert Smith</name>\n    <street>8 Oak Avenue</street>\n    <city>Old Town</city>\n    <state>PA</state>\n    <zip>95819</zip>\n  </billTo>\n  <comment>Hurry, my lawn is going wild!</comment>\n  <items>\n    <item partNum=\"872-AA\">\n      <productName>Lawnmower</productName>\n      <quantity>1</quantity>\n      <USPrice>148.95</USPrice>\n      <comment>Confirm this is electric</comment>\n    </item>\n    <item partNum=\"926-AA\">\n      <productName>Baby Monitor</productName>\n      <quantity>1</quantity>\n      <USPrice>39.98</USPrice>\n      <shipDate>1999-05-21</shipDate>\n    </item>\n  </items>\n</purchaseOrder>\n"

Note also that because the default toString() method first coerces to character, you could also use that.

identical(toString(dat), as.character(dat))
[1] TRUE
  • Related