【发布时间】:2018-02-21 13:40:59
【问题描述】:
我有大约 100 个发布数据的 XML 文件,每个文件 > 10GB,格式如下:
<?xml version="1.0" encoding="UTF-8"?>
<records xmlns="http://website”>
<REC rid=“this is a test”>
<UID>ABCD123</UID>
<data_1>
<fullrecord_metadata>
<references count=“3”>
<reference>
<uid>ABCD2345</uid>
</reference>
<reference>
<uid>ABCD3456</uid>
</reference>
<reference>
<uid>ABCD4567</uid>
</reference>
</references>
</fullrecord_metadata>
</data_1>
</REC>
<REC rid=“this is a test”>
<UID>XYZ0987</UID>
<data_1>
<fullrecord_metadata>
<references count=“N”>
</references>
</fullrecord_metadata>
</data_1>
</REC>
</records>
,每个唯一条目(由 UID 索引)的引用数量不同,其中一些可能为零。
目标:为每个 XML 文件创建 1 个简单的 data.frame,如下所示-
UID reference
ABCD123 ABCD2345
ABCD123 ABCD3456
ABCD123 ABCD4567
XYZ0987 NULL
由于文件的大小和需要高效循环许多文件,我一直在探索 xmlEventParse 以限制内存使用。我可以成功地为每个“REC”提取关键的唯一“UID”,并使用前面问题中的以下代码创建一个 data.frame:
branchFunction <- function() {
store <- new.env()
func <- function(x, ...) {
ns <- getNodeSet(x, path = "//UID")
key <- xmlValue(ns[[1]])
value <- xmlValue(ns[[1]])
print(value)
store[[key]] <- value
}
getStore <- function() { as.list(store) }
list(UID = func, getStore=getStore)
}
myfunctions <- branchFunction()
xmlEventParse(
file = "test.xml",
handlers = NULL,
branches = myfunctions
)
DF <- do.call(rbind.data.frame, myfunctions$getStore())
但我无法成功存储参考数据,也无法处理单个 UID 的参考编号变化。感谢您的任何建议!
【问题讨论】:
标签: r xml xml-parsing large-data