xml with nested siblings to data frame in R

前端 未结 1 940
有刺的猬
有刺的猬 2020-12-18 16:17

I am new to parsing XML in R. I am trying to parse XML into a workable data frame. I have tried some XPath functions from the XML package but cannot seem to arrive at the co

相关标签:
1条回答
  • 2020-12-18 16:35

    Considering xml holds your example string, here's another strategy for Residential Properties with a varying number of items:

    library(XML)
    library(plyr) 
    # xml <- '<ResidentialProperty>........'
    doc <- xmlParse(xml, asText =  TRUE)
    df <- do.call(rbind.fill, lapply(doc['//ResidentialProperty'], function(x) { 
      names <- xpathSApply(x, './/.', xmlName) 
      names <- names[which(names == "text") - 1]
      values <- xpathSApply(x, ".//text()", xmlValue)
      return(as.data.frame(t(setNames(values, names)), stringsAsFactors = FALSE))
    }))
    df
    #   StreetNumber StreetName StreetSuffix StateOrProvince        StatusChangeDate  Latitude   Longitude County SchoolDistrict        View YearBuilt                     InteriorFeatures   Name        Roof                                Exterior
    # 1        11111      111th    Avenue Ct              WA 2015-07-05T23:48:53.410 11.111111 -111.111111 Pierce       Puyallup Territorial      1997 Bath Off Master,Dbl Pane/Storm Windw Vacant Composition Brick,Cement Planked,Wood,Wood Products
    
    0 讨论(0)
提交回复
热议问题