前端之家收集整理的这篇文章主要介绍了
如何读取R中的大(~20 GB)xml文件?,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_
301_0@
我想从大型xml
文件(20 GB)中读取数据并对其进行操作.我厌倦了使用“xmlParse()”,但它在加载前给了我内存问题.有没有有效的
方法来做到这一点?
我的数据转储看起来像这样,
<tags>
<row Id="106929" TagName="moto-360" Count="1"/>
<row Id="106930" TagName="n1ql" Count="1"/>
<row Id="106931" TagName="fable" Count="1" ExcerptPostId="25824355" WikiPostId="25824354"/>
<row Id="106932" TagName="deeplearning4j" Count="1"/>
<row Id="106933" TagName="pystache" Count="1"/>
<row Id="106934" TagName="jitter" Count="1"/>
<row Id="106935" TagName="klein-mvc" Count="1"/>
</tags>
在XML包中,xmlEventParse
函数实现SAX(读取XML并
调用函数处理程序).如果您的XML足够简单(在一个根元素内重复元素),您可以使用branches参数为每个元素定义
函数.
例:
MedlineCitation = function(x,...) {
#This is a "branch" function
#x is a XML node - everything inside element <MedlineCitation>
# find element <ArticleTitle> inside and print it:
ns <- getNodeSet(x,path = "//ArticleTitle")
value <- xmlValue(ns[[1]])
print(value)
}
调用XML解析:
xmlEventParse(
file = "http://www.nlm.nih.gov/databases/dtd/medsamp2015.xml",handlers = NULL,branches = list(MedlineCitation = MedlineCitation)
)
关闭解决方案:
就像Martin Morgan,Storing-specific-xml-node-values-with-rs-xmleventparse:
branchFunction <- function() {
store <- new.env()
func <- function(x,...) {
ns <- getNodeSet(x,path = "//ArticleTitle")
value <- xmlValue(ns[[1]])
print(value)
# if storing something ...
# store[[some_key]] <- some_value
}
getStore <- function() { as.list(store) }
list(MedlineCitation = func,getStore=getStore)
}
myfunctions <- branchFunction()
xmlEventParse(
file = "medsamp2015.xml",branches = myfunctions
)
#to see what is inside
myfunctions$getStore()