xml2:如何将 xml 对象转换为字符串(在内存中,作为 R 对象)
我有以下情况:
- 我使用
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
xml2
包为其对象类提供as.character()
方法。因此,使用内置示例,您可以简单地执行以下操作:另请注意,因为默认的 toString() 方法首先强制转换为字符,所以您也可以使用它。
The
xml2
package providesas.character()
methods for its object classes. So, using a built-in example, you can simply do:Note also that because the default
toString()
method first coerces to character, you could also use that.