1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
# Adapt this to be able to specify an XPath expression to identify a list of nodes.
setGeneric("xmlToDataFrame",
#
# Read a relatively flat, 2-level deep XML document into a data frame.
# The document is assumed to be something of the form
# <top>
# <obs>
# <var1>value</var1>
# <var2>value</var2>
# <var3>value</var3>
# </obs>
# <obs>
# <var1>value</var1>
# <var2>value</var2>
# <var3>value</var3>
# </obs>
# </top>
#
# This can handle cases where not all observations have the same
# fields.
#
# z = xmlToDataFrame("~/size.xml")
# z = xmlToDataFrame("~/size.xml", c("integer", "integer", "numeric"))
#
function(doc, colClasses = NULL, homogeneous = NA, collectNames = TRUE, nodes = list(), stringsAsFactors = FALSE)
standardGeneric("xmlToDataFrame"))
setMethod("xmlToDataFrame", "character",
# parse the XML document if it is a file name and
# not a regular XML document already.
function(doc, colClasses = NULL, homogeneous = NA, collectNames = TRUE, nodes = list(), stringsAsFactors = FALSE)
xmlToDataFrame(xmlParse(doc), colClasses, homogeneous, collectNames, stringsAsFactors = stringsAsFactors))
setMethod("xmlToDataFrame", c("XMLInternalDocument", nodes = "missing"),
function(doc, colClasses = NULL, homogeneous = NA, collectNames = TRUE, nodes = list(), stringsAsFactors = FALSE)
xmlToDataFrame(doc, colClasses, homogeneous, collectNames, nodes = xmlChildren(xmlRoot(doc)), stringsAsFactors))
tmp =
function(doc, colClasses = NULL, homogeneous = NA, collectNames = TRUE, nodes = list(), stringsAsFactors = FALSE)
{
if(length(nodes) == 0)
return(data.frame())
# Find out how many fields there.
nfields = sapply(nodes, xmlSize)
nvar = max(nfields)
if(collectNames)
varNames = unique(unlist( lapply(nodes, names) ))
else
varNames = names(nodes[[which.max(nfields)]])
if(is.na(homogeneous))
homogeneous = all(nfields == nvar) && all(sapply(nodes[-1], function(x) all(names(x) == varNames)))
if(!homogeneous)
return(fromRaggedXML2DataFrame(nodes, varNames, c(length(nfields), length(varNames)), colClasses, stringsAsFactors))
# Function to operate on each
fun = function(x) {
tmp = xmlSApply(x, xmlValue)
length(tmp) = nvar
tmp
}
# Get the individual values
vals = unlist(lapply(nodes, fun))
ans = matrix(vals, length(nfields), byrow = TRUE)
ans =
if(length(colClasses)) {
as.data.frame(lapply(seq(along = colClasses),
function(i) {
as(ans[, i], colClasses[i])
}), stringsAsFactors = stringsAsFactors)
} else
as.data.frame(ans, stringsAsFactors = stringsAsFactors)
names(ans) = varNames
ans
}
bob =
function(doc, colClasses = NULL, homogeneous = NA, collectNames = TRUE, nodes = list(), stringsAsFactors = FALSE)
xmlToDataFrame(nodes = doc, colClasses = colClasses, homogeneous = homogeneous, collectNames = collectNames, stringsAsFactors = stringsAsFactors)
setMethod("xmlToDataFrame", c(nodes = "XMLNodeSet"), tmp)
setMethod("xmlToDataFrame", c(nodes = "list"), tmp)
setOldClass("XMLInternalNodeList")
setMethod("xmlToDataFrame", c(nodes = "XMLInternalNodeList"), tmp)
setMethod("xmlToDataFrame", "XMLNodeSet", bob)
setMethod("xmlToDataFrame", "XMLInternalNodeList", bob)
setMethod("xmlToDataFrame", "list", bob)
setMethod("xmlToDataFrame", "XMLInternalElementNode",
function(doc, colClasses = NULL, homogeneous = NA, collectNames = TRUE, nodes = list(), stringsAsFactors = FALSE)
xmlToDataFrame(nodes = xmlChildren(doc), colClasses = colClasses, homogeneous = homogeneous, collectNames = collectNames, stringsAsFactors = stringsAsFactors))
fromRaggedXML2DataFrame =
#
# This reads data from the nodes of an XML document and assumes
# that they do not all have the same number or even names of fields.
# So this does extra work to match each observation to the union of
# the field names across all nodes.
#
# o = fromRaggedXML2DataFrame("size2.xml")
# o = fromRaggedXML2DataFrame("size1.xml")
#
function(nodes, varNames = unique(unlist( lapply(nodes, names) )),
dims = c(length(nodes), length(varNames)), colClasses = NULL,
stringsAsFactors = FALSE)
{
#XXX
if(is.character(nodes))
nodes = xmlChildren(xmlRoot(xmlParse(nodes)))
# create an empty data frame with as many rows and columns as needed.
ans = as.data.frame(replicate(dims[2], rep(as.character(NA), dims[1]), simplify = FALSE), stringsAsFactors = FALSE)
names(ans) = varNames
# Fill in the rows based on the names.
for(i in seq(length = dims[1]))
ans[i, names(nodes[[i]])] = xmlSApply(nodes[[i]], xmlValue)
# Convert the columns to the specified classes if specified.
# Should drop cols with NULL. Also guess those with NA.
if(length(colClasses)) {
i = ! sapply(colClasses, is.null)
ans = ans[ i ]
varNames = varNames[i]
colClasses = colClasses[ i ]
ans = as.data.frame(lapply(seq(length = ncol(ans)),
function(i) {
as(ans[, i], colClasses[[i]])
}), stringsAsFactors = stringsAsFactors)
}
names(ans) = varNames
ans
}
setGeneric("xmlAttrsToDataFrame",
function(doc, attrs = character(), omit = character(), ...)
standardGeneric("xmlAttrsToDataFrame"))
setMethod("xmlAttrsToDataFrame", "character",
function(doc, attrs = character(), omit = character(), ...)
xmlAttrsToDataFrame(xmlParse(doc), attrs, omit, ...))
setMethod("xmlAttrsToDataFrame", "AsIs",
function(doc, attrs = character(), omit = character(), ...)
xmlAttrsToDataFrame(xmlParse(doc), attrs, omit, ...))
setMethod("xmlAttrsToDataFrame", "XMLInternalElementNode",
function(doc, attrs = character(), omit = character(), ...)
xmlAttrsToDataFrame(xmlChildren(doc), attrs, omit, ...))
setMethod("xmlAttrsToDataFrame", "XMLNodeSet",
function(doc, attrs = character(), omit = character(), ...) {
xmlAttrsToDataFrame(as(doc, 'list'), attrs, omit, ...)
})
setMethod("xmlAttrsToDataFrame", "list",
function(doc, attrs = character(), omit = character(), ...) {
# assuming these are all nodes.
combineNamedVectors(lapply(doc, xmlAttrs), attrs, omit, ...)
})
setMethod("xmlAttrsToDataFrame", "XMLInternalNodeList",
function(doc, attrs = character(), omit = character(), ...) {
# assuming these are all nodes.
combineNamedVectors(lapply(doc, xmlAttrs), attrs, omit, ...)
})
inAllRecords =
function(x)
{
tt = table(unlist(lapply(x, names)))
names(tt)[ tt == length(x)]
}
allNames =
function(x)
unique( unlist(lapply(x, names)) )
combineNamedVectors =
function(els, attrs = character(), omit = character(), ...)
{
if(is.function(attrs))
attrs = attrs(els)
if(!length(attrs)) {
attrs = allNames(els)
if(length(omit))
attrs = setdiff(attrs, omit)
}
if(length(attrs) == 0) {
warning("no elements to combine across records")
return(data.frame())
}
values = lapply(els, function(x) {
structure(x[attrs], names = attrs)
})
ans = as.data.frame(do.call(rbind, values), row.names = NULL, ...)
rownames(ans) = NULL
ans
}
|