Robert Luyt
Robert Luyt

Reputation: 399

R Get all information from specific elements of XML file

I have a XML file and I'm trying to get all the information from the element "identificatie" and "gebruiksdoelVerblijfsobject" and want to put them into a dataframe (even better a data.table but that's not mandatory). The XML file has a bunch of elements. I'll try to explain with my XML newbieness.

Here is the file itself: http://www.filedropper.com/xmlfile

The file structure is as following.

BAG-Extract-Deelbestand-LVC
    antwoord
        vraag
        producten
            LVC-product (list of 1774)
                Verblijfsobject
                    identificatie
                    gebruiksdoelVerblijfsobject

I am able to get a single "identificatie" and "gebruiksdoelVerblijfsobject" but I can't seem to get them all out of the XML file.

Could anybody please help me out? library(XML) is enabled.

Kind regards and thanks in advance,

Robert

Upvotes: 2

Views: 279

Answers (1)

jdharrison
jdharrison

Reputation: 30425

You need to use the namespaces provided:

xData <- xmlParse("PATH/ToFILE")
identificatie <- xpathSApply(xData, 
                             path = "//product_LVC:LVC-product/*/bag_LVC:identificatie"
                             , xmlValue)
> head(identificatie)
[1] "0362010002211033" "0362010002211034" "0362010002211035" "0362010002211036"
[5] "0362010002211037" "0362010002211038"

gebruiksdoelVerblijfsobject <- xpathSApply(xData, 
                                           path = "//product_LVC:LVC-product/*/bag_LVC:gebruiksdoelVerblijfsobject"
                                           , xmlValue)
> head(gebruiksdoelVerblijfsobject)
[1] "overige gebruiksfunctie" "overige gebruiksfunctie" "overige gebruiksfunctie"
[4] "overige gebruiksfunctie" "overige gebruiksfunctie" "overige gebruiksfunctie"
> unique(gebruiksdoelVerblijfsobject)
 [1] "overige gebruiksfunctie" "bijeenkomstfunctie"      "kantoorfunctie"         
 [4] "industriefunctie"        "sportfunctie"            "winkelfunctie"          
 [7] "woonfunctie"             "gezondheidszorgfunctie"  "logiesfunctie"          
[10] "onderwijsfunctie"  

EDIT: find non matches

voNodes <- getNodeSet(xData, path = "//product_LVC:LVC-product/bag_LVC:Verblijfsobject")

out <- lapply(voNodes, function(x){
  identificatie <- xpathSApply(x, "./bag_LVC:identificatie", xmlValue, namespaces= c(bag_LVC = "http://www.kadaster.nl/schemas/imbag/lvc/v20090901"))
  gebruiksdoelVerblijfsobject <- xpathSApply(x, path = "./bag_LVC:gebruiksdoelVerblijfsobject", xmlValue, namespaces= c(bag_LVC = "http://www.kadaster.nl/schemas/imbag/lvc/v20090901"))
  list(id = identificatie, gvo = gebruiksdoelVerblijfsobject)
})

> which(sapply(lapply(out, "[[", "gvo"), length) != 1)
 [1] 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1409 1410 1647
[18] 1648 1649 1708 1740

For these nodes listed above gvo has 2 entries for id's 1.

voNodes <- getNodeSet(xData, path = "//product_LVC:LVC-product/bag_LVC:Verblijfsobject")
# for each product get the
out <- lapply(voNodes, function(x){
  identificatie <- xpathSApply(x, "./bag_LVC:identificatie", xmlValue, namespaces= c(bag_LVC = "http://www.kadaster.nl/schemas/imbag/lvc/v20090901"))
  gebruiksdoelVerblijfsobject <- xpathSApply(x, path = "./bag_LVC:gebruiksdoelVerblijfsobject", xmlValue, namespaces= c(bag_LVC = "http://www.kadaster.nl/schemas/imbag/lvc/v20090901"))
  data.frame(id = identificatie, gvo1 = gebruiksdoelVerblijfsobject[1], gvo2 = gebruiksdoelVerblijfsobject[2])
})
require(plyr)
res <- rbind.fill(out)

> head(res)
                id                    gvo1 gvo2
1 0362010002211033 overige gebruiksfunctie <NA>
2 0362010002211034 overige gebruiksfunctie <NA>
3 0362010002211035 overige gebruiksfunctie <NA>
4 0362010002211036 overige gebruiksfunctie <NA>
5 0362010002211037 overige gebruiksfunctie <NA>
6 0362010002211038 overige gebruiksfunctie <NA>

Upvotes: 3

Related Questions