为了进行一些分析,我想使用 R 和 XML 包将 XML 导入数据帧。XML 文件示例:
<watchers shop_name="TEST" created_at="September 14, 2012 05:44">
<watcher channel="Site Name">
<code>123456</code>
<search_key>TestKey</search_key>
<date>September 14, 2012 04:15</date>
<result>Found</result>
<link>http://www.test.com/fakeurl</link>
<price>100.0</price>
<shipping>0.0</shipping>
<origposition>0</origposition>
<name>Name Test</name>
<results>
<result position="1">
<c_name>CTest1</c_name>
<c_price>599.49</c_price>
<c_shipping>0.0</c_shipping>
<c_total_price>599.49</c_total_price>
<c_rating>8.3</c_rating>
<c_delivery/>
</result><result position="2">
<c_name>CTest2</c_name>
<c_price>654.0</c_price>
<c_shipping>0.0</c_shipping>
<c_total_price>654.0</c_total_price>
<c_rating>9.8</c_rating>
<c_delivery/>
</result>
<result position="3">
<c_name>CTest3</c_name>
<c_price>654.0</c_price>
<c_shipping>0.0</c_shipping>
<c_total_price>654.0</c_total_price>
<c_rating>8.8</c_rating>
<c_delivery/>
</result>
</results>
</watcher>
</watchers>
Run Code Online (Sandbox Code Playgroud)
我想让数据框的行包含以下字段:
shop_name created_at code search_key date result
link price shipping origposition name
position c_name c_price c_shipping c_total_price
c_rating c_delivery
Run Code Online (Sandbox Code Playgroud)
这意味着还必须考虑子节点,这将导致本示例中的三行数据框(因为结果显示 3 个位置)。田野
shop_name created_at code search_key
date result link price shipping
origposition name
Run Code Online (Sandbox Code Playgroud)
这些行中的每一行都相同。
我能够通过 XML 文件,但我无法获得包含我想要的字段的数据框。当我将数据帧转换为数据帧时,我得到以下字段:
"code" "search_key" "date" "result"
"link" "price" "shipping" "origposition"
"name" "results"
Run Code Online (Sandbox Code Playgroud)
这里的字段
shop_name created_at
Run Code Online (Sandbox Code Playgroud)
开头缺少,并且“结果”放在“结果”列下的字符串中。
必须有可能获得想要的数据帧,但我不知道如何准确地做到这一点。
更新
@MvG 提供的解决方案在上述测试 XML 文件上表现出色。但是,“结果”列也可以具有“未找到”值。具有此值的条目将丢失某些字段(始终相同的字段),因此在运行解决方案时会产生“参数列数不匹配”错误。我也希望将这些条目放入数据框中,将不存在的字段留空。我不明白如何合并这个场景。
测试文件
<watchers shop_name="TEST" created_at="September 14, 2012 05:44">
<watcher channel="Site Name">
<code>123456</code>
<search_key>TestKey</search_key>
<date>September 14, 2012 04:15</date>
<result>Found</result>
<link>http://www.test.com/fakeurl</link>
<price>100.0</price>
<shipping>0.0</shipping>
<origposition>0</origposition>
<name>Name Test</name>
<results>
<result position="1">
<c_name>CTest1</c_name>
<c_price>599.49</c_price>
<c_shipping>0.0</c_shipping>
<c_total_price>599.49</c_total_price>
<c_rating>8.3</c_rating>
<c_delivery/>
</result><result position="2">
<c_name>CTest2</c_name>
<c_price>654.0</c_price>
<c_shipping>0.0</c_shipping>
<c_total_price>654.0</c_total_price>
<c_rating>9.8</c_rating>
<c_delivery/>
</result>
<result position="3">
<c_name>CTest3</c_name>
<c_price>654.0</c_price>
<c_shipping>0.0</c_shipping>
<c_total_price>654.0</c_total_price>
<c_rating>8.8</c_rating>
<c_delivery/>
</result>
</results>
</watcher>
<watcher channel="Shopping">
<code>12804</code>
<search_key></search_key>
<date></date>
<result>Not found</result>
<link>https://www.test.com/testing1323p</link>
<price>0.0</price>
<shipping>0.0</shipping>
<origposition>0</origposition>
<name>MOOVM6002020</name>
<results>
</results>
</watcher>
</watchers>
Run Code Online (Sandbox Code Playgroud)
这是一种更通用的方法。每个节点都分为以下三种情况之一:
rows,则来自子节点的数据帧将导致结果的不同行。cols,那么来自子节点的数据帧将产生结果的不同列。value,那么将构造一个具有单个值的数据框,使用节点名称作为列名称,使用节点值作为列值。您的申请电话位于底部。
library(XML)
zeroColSingleRow <- function() {
res <- data.frame(dummy=NA)
res$dummy <- NULL
stopifnot(nrow(res) == 1, ncol(res) == 0)
return (res)
}
xml2df <- function(node, classifier) {
if (! inherits(node, c("XMLInternalElementNode", "XMLElementNode"))) {
return (zeroColSingleRow())
}
kind <- classifier(node)
if (kind == "rows") {
cdf <- lapply(xmlChildren(node), xml2df, classifier)
if (length(cdf) == 0) {
res <- zeroColSingleRow()
}
else {
names <- unique(unlist(lapply(cdf, colnames)))
cdf <- lapply(cdf, function(i) {
missing <- setdiff(names, colnames(i))
if (length(missing) > 0) {
i[missing] <- NA
}
return (i)
})
res <- do.call(rbind, cdf)
}
}
else if (kind == "cols") {
cdf <- lapply(xmlChildren(node), xml2df, classifier)
if (length(cdf) == 0) {
res <- zeroColSingleRow()
}
else {
res <- cdf[[1]]
if (length(cdf) > 1) {
for (i in 2:length(cdf)) {
res <- merge(res, cdf[[i]], by=NULL)
}
}
}
}
else {
stopifnot(kind == "value")
res <- data.frame(xmlValue(node))
names(res) <- xmlName(node)
}
if (ncol(res) == 0) {
res <- zeroColSingleRow()
}
attr <- xmlAttrs(node)
if (length(attr) > 0) {
attr <- do.call(data.frame, as.list(attr))
res <- merge(attr, res, by=NULL)
}
rownames(res) <- NULL
return(res)
}
doc<-xmlParse("test.xml")
xml2df(xmlRoot(doc), function(node) {
name <- xmlName(node)
if (name %in% c("watchers", "results"))
return("rows")
# make sure to treat results/result different from watcher/result
if (name %in% c("watcher", "result") &&
xmlName(xmlParent(node)) == paste0(name, "s"))
return("cols")
return("value")
})
Run Code Online (Sandbox Code Playgroud)