R from XML when values ​​are few or absent

This question is similar to the previous question Import all XML fields (and subfields) as a data framework , but I want to pull out only a subset of XML data and want to include missing / multiple values.

I start with an XML file and want to build a dataframe in R based on some of the data it contains, defined by the contents of the XML elements. This is easiest to explain with an example. Below I want to highlight information about the attractions for each city (even if there is no indicative element or several of them) and ignore information about the stations.

<world>
    <city>
        <name>London</name>
        <buildings>
            <building>
                <type>landmark</type>
                <bname>Tower Bridge</bname>
            </building>
            <building>
                <type>station</type>
                <bname>Waterloo</bname>
            </building>
        </buildings>
    </city>
    <city>
        <name>New York</name>
        <buildings>
            <building>
                <type>station</type>
                <bname>Grand Central</bname>
            </building>
        </buildings>
    </city>
    <city>
        <name>Paris</name>
        <buildings>
            <building>
                <type>landmark</type>
                <bname>Eiffel Tower</bname>
            </building>
            <building>
                <type>landmark</type>
                <bname>Louvre</bname>
            </building>
        </buildings>
    </city>
</world>

Ideally, this will fall into a data framework that looks something like this:

 London      Tower Bridge
 New York    NA
 Paris       Eiffel Tower
 Paris       Louvre

, , XML xpathSApply, , .

, , , , .

+3
4

, XML world.xml, , name bname :

library(XML)
doc <- xmlParse("world.xml", useInternalNodes = TRUE)

do.call(rbind, xpathApply(doc, "/world/city", function(node) {

   city <- xmlValue(node[["name"]])

   xp <- "./buildings/building[./type/text()='landmark']/bname"
   landmark <- xpathSApply(node, xp, xmlValue)
   if (is.null(landmark)) landmark <- NA

   data.frame(city, landmark, stringsAsFactors = FALSE)

}))

:

      city     landmark
1   London Tower Bridge
2 New York         <NA>
3    Paris Eiffel Tower
4    Paris       Louvre
+5

xmlToList, plyr, ,

require(XML)
require(plyr)
xD <- xmlParse(xData)
xL <- xmlToList(xD)
ldply(xL, data.frame)
> ldply(xL, data.frame)
   .id     name buildings.building.type buildings.building.bname
1 city   London                landmark             Tower Bridge
2 city New York                 station            Grand Central
3 city    Paris                landmark             Eiffel Tower
  buildings.building.type.1 buildings.building.bname.1
1                   station                   Waterloo
2                      <NA>                       <NA>
3                  landmark                     Louvre

, ,

+2

xpathSapply, xpath . , xmlToDataFrame .

dd <- xmlToDataFrame(doc)
rr <- gsub('landmark',',',dd$buildings)
rr <- gsub('station.*','',rr)
builds <- lapply(strsplit(gsub('station.*','',rr),','),
                 function(x)x[nchar(x)>0])
dd$buildings <- builds

    name            buildings
1   London         Tower Bridge
2 New York                     
3    Paris Eiffel Tower, Louvre
0

, , XML , :

xml_list <- xmlToList(xmlParse(xml_data))

"" node , "":

xml_list <- lapply(xml_list, lapply, function(x) {
  x[!sapply(x, function(y) any(y == "station"))]
})

xml_list <- lapply(xml_list, function(x) {
  bldgs <- unlist(x$buildings)
  bldgs <- bldgs[bldgs != "landmark"]
  if(is.null(bldgs)) bldgs <- NA
  data.frame(
    "city" = x$name,
    "landmark" = bldgs,
    stringsAsFactors = FALSE)
})

:

xml_output <- do.call("rbind", xml_list)
xml_output
           city     landmark
city     London Tower Bridge
city1  New York         <NA>
city.1    Paris Eiffel Tower
city.2    Paris       Louvre
0
source

Source: https://habr.com/ru/post/1612524/


All Articles