Home > Software engineering >  Parsing big XML with XML2 in R
Parsing big XML with XML2 in R

Time:12-01

I'm working with a big XML files regarding the weather and I need somehow to get information from all nodes. Each XML consist in a month which contains every day divided in 10 minutes interval.

The XML looks like this but much longer:

<?xml version= "1.0" encoding="ISO-8859-1" ?>
<mes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="G039_2003_1.xsd">
    <dia Dia="2003-1-01">
        <hora Hora="00:00">
            <Meteoros>
                <Cub.Vto._a_3050cm>0.59</Cub.Vto._a_3050cm>
                <Dir.Med._a_3050cm>215.0</Dir.Med._a_3050cm>
                <Humedad._a_3050cm>56.0</Humedad._a_3050cm>
                <Irradia.._a_800cm>2.0</Irradia.._a_800cm>
                <Precip.._a_174cm>0.0</Precip.._a_174cm>
                <Presión._a_60cm>800.1</Presión._a_60cm>
                <Sig.Dir._a_3050cm>15.0</Sig.Dir._a_3050cm>
                <Sig.Vel._a_3050cm>20.0</Sig.Vel._a_3050cm>
                <Tem.Sue._a_0cm>11.4</Tem.Sue._a_0cm>
                <Tem.Aire._a_164cm>14.5</Tem.Aire._a_164cm>
                <Vel.Max._a_3050cm>13.9</Vel.Max._a_3050cm>
                <Vel.Med._a_3050cm>7.9</Vel.Med._a_3050cm>
            </Meteoros>
        </hora>
        <hora Hora="00:10">
            <Meteoros>
                <Cub.Vto._a_3050cm>0.39</Cub.Vto._a_3050cm>
                <Dir.Med._a_3050cm>211.0</Dir.Med._a_3050cm>
                <Humedad._a_3050cm>58.0</Humedad._a_3050cm>
                <Irradia.._a_800cm>1.0</Irradia.._a_800cm>
                <Precip.._a_174cm>0.0</Precip.._a_174cm>
                <Presión._a_60cm>800.1</Presión._a_60cm>
                <Sig.Dir._a_3050cm>15.0</Sig.Dir._a_3050cm>
                <Sig.Vel._a_3050cm>18.0</Sig.Vel._a_3050cm>
                <Tem.Sue._a_0cm>11.0</Tem.Sue._a_0cm>
                <Tem.Aire._a_164cm>14.5</Tem.Aire._a_164cm>
                <Vel.Max._a_3050cm>12.2</Vel.Max._a_3050cm>
                <Vel.Med._a_3050cm>6.8</Vel.Med._a_3050cm>
            </Meteoros>

So basically what I need is an output of the date, the hour and the information every time.

I tried this so far:

library(XML)
library(xml2)
setwd()
Enero2003 <- read_xml("C039_2003/G039_2003_1.xml")

That, gives this a result:

enter image description here

I need a loop that gives me date, hour, and the data inside of each node. For that I've tried this, and is not working.

MonthDays <-length(xml_contents(Enero2003))
  for(i in 1:MonthDays) {
    xml_child(xml_child(xml_child(Enero2003, 1), 1), 1)}

HourOfTheDay <- xml_attrs(xml_child(xml_child(Enero2003, 1), 1))[["Hora"]]

I'll appreciate any help. Thank you.

CodePudding user response:

This should get you started....

library(xml2)
library(magrittr)
# read xml
doc <- xml2::read_xml(xml.text)
# get all  meteoros-nodes
nodes <- xml2::xml_find_all(doc, ".//Meteoros")
# get all possible childersn-names of the meteoros-nodes
cols <- xml_children(nodes) %>% xml2::xml_name() %>% unique()
# initialise matrix
p <- matrix(nrow = length(nodes), ncol = length(cols))
# fill matrix childnodes
for (i in 1:length(cols) ) {
  p[, i] <- xml2::xml_find_first(nodes, paste0( ".//", cols[i])) %>% xml2::xml_text()
}
colnames(p) <- cols
# get day   time info
q <- matrix(nrow = length(nodes), ncol = 2)
q[, 1] <- xml2::xml_find_first(nodes, "./ancestor::hora") %>% xml2::xml_attr("Hora")
q[, 2] <- xml2::xml_find_first(nodes, "./ancestor::dia") %>% xml2::xml_attr("Dia")
colnames(q) <- c("Hora", "Dia")
final <- as.data.frame(cbind(q, p))
  • Related