Reputation: 692
I have the following situation:
xml2::read_xml
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)
Writing to disk something that could be done in memory is very inefficient.
Am I missing an obvious function in the xml2 package?
Upvotes: 2
Views: 1304
Reputation: 34501
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
Upvotes: 3