user5831311
user5831311

Reputation: 273

xml to R dataframe

I am trying to store all the data in the below xml file as 1 or numerous data frames in R. So far, only able to parse the first SubCategory and store it in a df. Would like to store all the further data in the xml file in 1 or numerous dataframes - whichever is an easier solution. Any suggestions?

My code:

library(XML)
data<-xmlParse("http://advisory.mtanyct.info/LPUWebServices/CurrentLostProperty.aspx")
xml_data <- xmlToList(data)
SubCategory<- data.frame(as.list(xml_data[["Category"]][["SubCategory"]]))

Upvotes: 0

Views: 795

Answers (1)

Chris S.
Chris S.

Reputation: 2225

Try xmlAttrsToDataFrame

x <- lapply(data["//Category"], XML:::xmlAttrsToDataFrame)
names(x) <- xpathSApply(data, "//Category", xmlGetAttr, "Category")
library(plyr)
ldply(x, data.frame, .id="Category")
           Category              SubCategory count
1  Home Furnishings Wall and Window Covering    79
2  Home Furnishings                Ornaments    49
3  Home Furnishings               Appliances    97
4  Home Furnishings                    Linen   971
5  Home Furnishings           Floor Covering    30
6  Home Furnishings    All Other Furnishings   557
7  Home Furnishings                 Dishware   609
8  Sports Equipment                     Bats    13
9  Sports Equipment                  Scooter    64
10 Sports Equipment                Golf Club     5
...
221        Eye Wear               Sunglasses  2671

Upvotes: 1

Related Questions