我是XML新手,我知道在处理大文件时,最好在R中使用xmlEventParse()和getNodeSet()。但是,然后,我的代码以endlesly运行,而我没有知道为什么。也许不正确的路径定义?xmlEventParse返回空数据框
,看起来像原始文件一个虚拟的XML文件,可以在此链接上找到: dummy xml file
而且我哑R代码里面是这样的:
library(XML)
FOOid_traverse <- function() {
uids <- c()
refs <- c()
REC <- function(x) {
uid <- xpathSApply(x, "//N8:EntityList/N8:Entity/N2:OrganisationName",
xmlValue)
ref <- xpathSApply(x, "//N8:EntityList/N8:Entity/N5:Identifiers/N5:Identifier/N5:IdentifierElement", xmlValue)
if (length(uid) > 0) {
if (length(ref) == 0) {
uids <<- c(uids, uid)
refs <<- c(refs, NA_character_)
} else {
uids <<- c(uids, rep(uid, length(ref)))
refs <<- c(refs, ref)
}
}
}
list(
REC = REC,
FOOid_df = function() {
data.frame(uid = uids, ref = refs, stringsAsFactors = FALSE)
}
)
}
FOOid_f < - FOOid_traverse()
无形( xmlEventParse( 文件= path.expand( “companies_xml_extract_20170703.xml”), 分支= FOOid_f [ “REC”]) )
FOOid_f $ FOOid_df()
由于
你能请解释一下你的期望在平原的话呢? – Prem
我期望一个具有OrganisationName&IdentifierElement作为列头的数据框。换句话说,我期望提取存储在“N8:实体列表/ N8:实体/ N2:组织名称”和“N8:实体列表/ N8:实体/ N5:标识符/ N5:标识符/ N5:标识符元素”路径中的数据 – William
Isn' xml不完整?您可能需要在帖子中更正它。 – Prem