我正在使用该xml2
包将一个巨大的 XML 文件读入内存,但该命令失败并出现以下错误:
错误:字符 0x0 超出允许范围 [9]
我的代码如下所示:
library(xml2)
doc <- read_xml('~/Downloads/FBrf.xml')
数据可以在ftp://ftp.flybase.net/releases/FB2015_05/reporting-xml/FBrf.xml.gz 下载(大约 140MB),解压后大约有 1.8GB。
有没有人建议如何在阅读之前找出哪些字符有问题或如何清理文件。
编辑
好的,因为文件很大,所以我搜索了有关堆栈溢出的其他解决方案,并尝试实现 Martin Morgan 的解决方案,他在这里介绍了将值合并到巨大的 XML 文件中
所以到目前为止我所做的是以下代码行
library(XML)
branchFunction <- function(progress=10) {
res <- new.env(parent=emptyenv()) # for results
it <- 0L # iterator -- nodes visited
list(publication=function(elt) {
## handle 'publication' nodes
if (getNodeSet(elt, "not(/publication/feature/id)"))
## early exit -- no feature id
return(NULL)
it <<- it + 1L
if (it %% progress == 0L)
message(it)
publication <- getNodeSet(elt, "string(/publication/id/text())") # 'key'
res[[publication]] <-
list(miniref=getNodeSet(elt,
"normalize-space(/publication/miniref/text())"),
features= xpathSApply(elt, "//feature/id/text()", xmlValue))
}, getres = function() {
## retrieve the 'res' environment when done
res
}, get=function() {
## retrieve 'res' environment as data.frame
publication <- ls(res)
miniref <- unlist(eapply(res, "[[", "miniref"), use.names=FALSE)
feature <- eapply(res, "[[", "features")
len <- sapply(feature, length)
data.frame(publication=rep(publication, len),
feature=unlist(feature, use.names=FALSE),
miniref=rep(miniref, len))
})
}
branches <- branchFunction()
xmlEventParse("~/Downloads/jnk.xml", handlers=NULL, branches=branches)
# xmlEventParse("~/Downloads/FBrf.xml", handlers=NULL, branches=branches)
branches$get()
我将 xml 文件上传到我的服务器http://download.dejung.net/jnk.xml
该文件只有几 kb,但问题是结果。第二个发布条目有一个 id FBrf0162243和一个 miniref Schwartz et al., 2003, Mol. Cell. Biol. 23(19): 6876--6886
。
我上面发布的代码的结果向相应的 miniref 报告了错误的发布 ID。特征ID是正确的......
FBrf0050934 FBgn0003277 Schwartz 等人,2003 年,摩尔。细胞。生物学。23(19): 6876--6886
不知道为什么我的代码报告了错误的值,也许有人可以帮助我关闭闭包,因为这对我来说很新。