I have a lot of XML files (about 100,000) that look like this. Each file has about 100 node points. I show only five of them for illustration.
<?xml version="1.0" encoding="UTF-8"?>
-<car id="id1">
<point time="1272686841" lon="-122.40648" lat="37.79778" status="E" unit="id1"/>
<point time="1272686781" lon="-122.40544" lat="37.79714" status="M" unit="id1"/>
<point time="1272686722" lon="-122.40714" lat="37.79774" status="M" unit="id1"/>
<point time="1272686661" lon="-122.40704" lat="37.7976" status="M" unit="id1"/>
<point time="1272686619" lon="-122.40616" lat="37.79698" status="E" unit="id1"/>
</car>
I want to combine all these XML files into one big data frame (about 100,000x100 = 10,000,000 rows) in R with five columns (time, bos, lat, unit, status). All files have the same five variables, but can be in a different order.
Below is my code. First, I create five vectors to save these five variables. Then I go to each file, read the records one by one.
setwd("C:\\Users\\MyName\\Desktop\\XMLTest")
all.files <- list.files()
n <- 2000000
all.lon <- rep(NA, n)
all.lat <- rep(NA, n)
all.time <- rep(NA, n)
all.status <- rep(NA, n)
all.unit <- rep(NA, n)
i <- 1
for (cur.file in all.files) {
if (tolower(file_ext(cur.file)) == "xml") {
xmlfile <- xmlTreeParse(cur.file)
xmltop <- xmlRoot(xmlfile)
for (j in 1:length(xmltop)) {
cur.node <- xmltop[[j]]
cur.lon <- as.numeric(xmlGetAttr(cur.node, "lon"))
cur.lat <- as.numeric(xmlGetAttr(cur.node, "lat"))
cur.time <- as.numeric(xmlGetAttr(cur.node, "time"))
cur.unit <- xmlGetAttr(cur.node, "unit")
cur.status <- xmlGetAttr(cur.node, "status")
all.lon[i] <- cur.lon
all.lat[i] <- cur.lat
all.time[i] <- cur.time
all.status[i] <- cur.status
all.unit[i] <- cur.unit
i <- i + 1
}
}
}
XML, , . , . - . - for for (j in 1:length(xmltop)) . xmlToDataFrame, .
> xmlToDataFrame(cur.file)
Error in matrix(vals, length(nfields), byrow = TRUE) :
'data' must be of a vector type, was 'NULL'
?