在 R 中,如何将两个 XML 文档合并为一个文档? [英] In R, how do I combine two XML documents into one document?
问题描述
我正在从基于 XML 的 API 查询数据.API 响应是分页的,因此我必须进行大量查询才能获得完整的数据集.
I'm querying data from an XML-based API. The API responses are paginated, so I have to make a bunch of queries to get the full data set.
使用 xml2
包中的 read_xml
,我可以轻松地发出每个请求并将其保存为 XML 文档,但我一直无法弄清楚如何使用库将它们合并为一个文档.(我想这样做,以便我可以进行我需要进行一次而不是 50 次的 Xpath 查询.)
Using read_xml
from the xml2
package, I can easily make each request and save it as an XML document, but I've been having trouble figuring out how to use the library to combine them into one document. (I would like to do this so I can make the Xpath queries I need to make once instead of 50 times.)
我尝试创建一个新的空白文档并将其他人的节点添加为元素,但是 xml_add_child
和 xml_add_sibling
函数都将第二个文档作为参数,并且似乎都不喜欢传递 xml_find_all
查询的结果.(他们抱怨无法处理参考文献.)
I've tried creating a new blank document and adding the nodes of others as elements, but the xml_add_child
nor the xml_add_sibling
functions will take a second document as an argument, and neither seem to like being passed the result of an xml_find_all
query. (They complain about not being able to work with references.)
所以,我很难过.
(注意:我也没有成功地发现如何使用原始 XML
包执行此操作.)
(Note: I've also not had any success in discovering how to do this with the original XML
package.)
推荐答案
经过反复试验,我想出了如何使用 xml2
包来做到这一点.
After some trial and error, I've figured out how to do this with the xml2
package.
让我们考虑将两个非常简单的 XML 文档组合在一起的简单情况.
Let us consider the simple case of two very simple XML documents we'd like to combine together.
doc1 <- read_xml("<items><item>1</item><item>2</item><items>")
doc2 <- read_xml("<items><item>3</item><item>4</item><items>")
(注意:文档来自哪里并不重要,read_xml
的参数是它可以读取的任何内容.)
(Note: where the documents come from don't matter, the argument to read_xml
is anything it can read.)
要将它们组合在一起,只需执行以下操作:
To combine them together, simply do the following:
doc2children <- xml_children(doc2)
for (child in doc2children) {
xml_add_child(doc1, child)
}
现在,当您查看 doc1 时,您应该看到:
Now when you look at doc1 you should see this:
> doc1
{xml_document}
<items>
[1] <item>\n 1</item>
[2] <item>\n 2</item>
[3] <item>\n 3</item>
[4] <item>\n 4</item>
这篇关于在 R 中,如何将两个 XML 文档合并为一个文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!