2016-03-02 87 views
0

我試圖將所有數據存儲在下面的xml文件中作爲1或R中的無數數據幀。到目前爲止,只能解析第一個子類別並將其存儲在df中。想要將所有進一步的數據存儲在xml文件中1或多個數據框中 - 以較簡單的解決方案爲準。有什麼建議麼?xml到R dataframe

我的代碼:

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

回答

1

嘗試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 
+0

感謝克里斯,效果很好。爲了將頂層節點信息存儲到df中(NumberOfLostArticles和NumberOfItemsclaimed),我嘗試了以下操作,但沒有解決。你能提供你的想法嗎? – user5831311

+0

y < - lapply(data [「/ LostProperty」],XML ::: xmlAttrsToDataFrame) names(y)< - xpathSApply <-dply(y,data.frame,.id =「LostProperty」) – user5831311

+0

只需檢查XML文件。像'Category'一樣,這兩個節點是LostProperty的子節點,但沒有屬性,所以可以試試'xpathSApply(data,「// NumberOfLostArticles」,xmlValue) –