【发布时间】:2015-08-27 05:14:05
【问题描述】:
我想将我的 xml 读入 r 中的数据框。我的初始数据文件是 14 GB,所以我最初尝试读取文件没有成功:
f=xmlParse("Final.xml")
df=xmlToDataFrame(f)
r=xmlRoot(f)
问题是它总是内存不足....
我也看到了这个问题:
How to read large (~20 GB) xml file in R?
我尝试使用 Martin Morgan 的方法,虽然我没有 100% 理解但尝试将其应用于我的数据集。
libary(XML)
branchFunction <- function() {
store <- new.env()
func <- function(x, ...) {
ns <- getNodeSet(x, path = "//Sentiment")
value <- xmlValue(ns[[1]])
print(value)
# if storing something ...
# store[[some_key]] <- some_value
}
getStore <- function() { as.List(store) }
list(ROW = func, getStore=getStore)
}
myfunctions <- branchFunction()
xmlEventParse(
file = "Inputfile.xml",
handlers = NULL,
branches = myfunctions
))
myfunctions$getStore()
我必须对每个列分别执行此操作,并且我从输出中获得的结构没有用。
我的数据结构如下:
<ROWSET>
<ROW>
<Field1>21706</Field1>
<PostId>19203</PostId>
<ThreadId>38</ThreadId>
<UserId>1397</UserId>
<TimeStamp>1407351854</TimeStamp>
<Upvotes>0</Upvotes>
<Downvotes>0</Downvotes>
<Flagged>f</Flagged>
<Approved>t</Approved>
<Deleted>f</Deleted>
<Replies>0</Replies>
<ReplyTo>egergeg</ReplyTo>
<Content>dsfg</Content>
<Sentiment>Neutral</Sentiment>
</ROW>
<ROW>
<Field1>217</Field1>
<PostId>1903</PostId>
<ThreadId>8</ThreadId>
<UserId>197</UserId>
<TimeStamp>1407351854</TimeStamp>
<Upvotes>0</Upvotes>
<Downvotes>0</Downvotes>
<Flagged>f</Flagged>
<Approved>t</Approved>
<Deleted>f</Deleted>
<Replies>0</Replies>
<ReplyTo>sdrwer</ReplyTo>
<Content>wer</Content>
<Sentiment>Neutral</Sentiment>
</ROW>
<ROW>
<Field1>21306</Field1>
<PostId>19103</PostId>
<ThreadId>78</ThreadId>
<UserId>13497</UserId>
<TimeStamp>1407321854</TimeStamp>
<Upvotes>0</Upvotes>
<Downvotes>0</Downvotes>
<Flagged>f</Flagged>
<Approved>t</Approved>
<Deleted>f</Deleted>
<Replies>0</Replies>
<ReplyTo>tzjtj</ReplyTo>
<Content>rtgr</Content>
<Sentiment>Neutral</Sentiment>
</ROW>
</ROWSET>
【问题讨论】:
-
您应该提供一个完整 XML 的最小示例,否则其他人很难测试他们建议的解决方案。
-
我扩展了示例xml,似乎在我发布它时,xml的一部分被剪掉了。
标签: r xml large-data