xml2:如何将 xml 对象转换为字符串(在内存中,作为 R 对象)

发布于 2025-01-12 08:28:11 字数 497 浏览 3 评论 0原文

我有以下情况:

  • 我使用 xml2::read_xml 解析 xml 文件
  • 我更改了 xml 文档中的一些内容。详细信息与问题无关。
  • 现在我想将处理后的 xml 数据发送到 API。因此我需要该 xml 数据的文本表示。为此,XML 包具有函数 XML::toString。我在 xml2-Package 中找不到等效项。因此我这样做:
    tmpfile <- tempfile(fileext = ".xml")
    xml2::write_xml(xml, 文件 = tmpfile)
    txt <- readLines(tmpfile, 编码 = "UTF-8")
    文件.删除(tmpfile)
    call_api(txt)
    
  • 我正在为数百个文件执行此操作。

将可以在内存中完成的事情写入磁盘的效率非常低。

我是否缺少 xml2 包中的一个明显功能?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断念 2025-01-19 08:28:11

xml2 包为其对象类提供 as.character() 方法。因此,使用内置示例,您可以简单地执行以下操作:

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"

另请注意,因为默认的 toString() 方法首先强制转换为字符,所以您也可以使用它。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文