R xml树到数据帧
我有以下几点R xml树到数据帧,xml,r,xpath,xml-parsing,Xml,R,Xpath,Xml Parsing,我有以下几点 库(“XML”) 图书馆(“RCurl”) url对于每个OperationHeader获取其名称(opName)和所有计划的名称(plan),为每个OperationHeader创建一个组件列表。最后,rbind将组件放在一起: L <- xpathSApply(OperationList, "//OperationHeader", function(x) cbind(opName = xmlValue(x[["name"]]),
库(“XML”)
图书馆(“RCurl”)
url对于每个OperationHeader
获取其名称(opName)和所有计划的名称(plan),为每个OperationHeader创建一个组件列表。最后,rbind
将组件放在一起:
L <- xpathSApply(OperationList, "//OperationHeader", function(x)
cbind(opName = xmlValue(x[["name"]]),
plan = xpathSApply(x, "plans/PlanHeader/name", xmlValue)
)
)
do.call("rbind", L)
planname <- data.frame(sapply(OperationList["//subgroups/OperationGroup/subgroups/OperationGroup/operations/OperationHeader/plans/PlanHeader/name"], xmlValue))
operationanme <- data.frame(sapply(OperationList["//subgroups/OperationGroup/subgroups/OperationGroup/operations/OperationHeader/name"], xmlValue))
Operation.df1 <- xmlToDataFrame(OperationList)
require(XML)
xmlToDF = function(doc, xpath, isXML = TRUE, usewhich = TRUE, verbose = TRUE) {
if (!isXML)
doc = xmlParse(doc)
#### get the records for that form
nodeset <- getNodeSet(doc, xpath)
## get the field names
var.names <- lapply(nodeset, names)
## get the total fields that are in any record
fields = unique(unlist(var.names))
## extract the values from all fields
dl = lapply(fields, function(x) {
if (verbose)
print(paste0(" ", x))
xpathSApply(proc, paste0(xpath, "/", x), xmlValue)
})
## make logical matrix whether each record had that field
name.mat = t(sapply(var.names, function(x) fields %in% x))
df = data.frame(matrix(NA, nrow = nrow(name.mat), ncol = ncol(name.mat)))
names(df) = fields
## fill in that data.frame
for (icol in 1:ncol(name.mat)) {
rep.rows = name.mat[, icol]
if (usewhich)
rep.rows = which(rep.rows)
df[rep.rows, icol] = dl[[icol]]
}
return(df)
}
Operation.df2 <- xmlToDF(OperationList,
xpath = "/subgroups/OperationGroup/subgroups/OperationGroup/name")
require(XML)
Operation.df3 <- xpathApply(OperationList,
"/subgroups/OperationGroup/subgroups/OperationGroup/name",
function(node) {
region <- xmlValue(node[["name"]])
xp <- "./operations/OperationHeader/name"
operation <- xpathSApply(node, xp, xmlValue)
if (is.null(operation)) operation <- NA
data.frame(region, operation, stringsAsFactors = FALSE)
})
do.call(rbind, Operation.df3 )
require(data.table)
Operation.df41 <- data.frame(rbindlist(OperationList2))
Operation.df42 <- rbindlist(OperationList2)
Operation.df43 <- data.frame(matrix(unlist(OperationList2),
byrow=T),stringsAsFactors=FALSE)
Operation.df44 <- lapply(OperationList2, data.frame,
stringsAsFactors = FALSE)
Operation.df45 <- rbind.fill(Operation.df44)
xp <- function (OperationList, tag){
n <- xpathSApply(OperationList, tag, xmlValue)
if (length(n) > 0)
# paste multiple values?
paste0(n, collapse="; ")
else NA
}
z <- getNodeSet(OperationList, "//subgroups/OperationGroup/subgroups/OperationGroup")
n <-length(z)
notices <-vector("list",n)
for(i in 1:n)
{
Operation.df5<-xmlDoc(z[[i]])
Operation.df5[[i]] <- data.frame(
region = xp(z2, "//name"),
operation = xp(z2, "//operations/OperationHeader/name"),
stringsAsFactors=FALSE)
free(Operation.df5)
}
do.call("rbind", Operation.df5)
for (i in 1:length(getNodeSet(OperationList, "//subgroups/OperationGroup")))
{
if (i==1) {
foo<-xmlSApply(OperationList[[i]], xmlValue)
Operation.df6 <-data.frame(t(foo), stringsAsFactors=FALSE)
}
else {
foo<-xmlSApply(OperationList[[i]], xmlValue)
tmp<-data.frame(t(foo), stringsAsFactors=FALSE)
Operation.df6 <-rbind(Operation.df6, tmp)
}
}
L <- xpathSApply(OperationList, "//OperationHeader", function(x)
cbind(opName = xmlValue(x[["name"]]),
plan = xpathSApply(x, "plans/PlanHeader/name", xmlValue)
)
)
do.call("rbind", L)
opName plan
[1,] "State A" "Target Plan"
[2,] "State A" "Revision"
[3,] "State B" "Target Plan"
[4,] "Avgh" "Target Plan"
[5,] "Alaska" "Target Plan"
[6,] "Alaska" "Revision"