require(XML)
## Loading required package: XML
require(plyr)
## Loading required package: plyr
fileurl <- "http://www.w3schools.com/xml/simple.xml"
doc <- xmlParse(fileurl,useInternalNodes = TRUE) ### xmlParse()- is to parse the xml content, the parsed content is stored into doc
doc
## <?xml version="1.0" encoding="UTF-8"?>
## <breakfast_menu>
## <food>
## <name>Belgian Waffles</name>
## <price>$5.95</price>
## <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
## <calories>650</calories>
## </food>
## <food>
## <name>Strawberry Belgian Waffles</name>
## <price>$7.95</price>
## <description>Light Belgian waffles covered with strawberries and whipped cream</description>
## <calories>900</calories>
## </food>
## <food>
## <name>Berry-Berry Belgian Waffles</name>
## <price>$8.95</price>
## <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
## <calories>900</calories>
## </food>
## <food>
## <name>French Toast</name>
## <price>$4.50</price>
## <description>Thick slices made from our homemade sourdough bread</description>
## <calories>600</calories>
## </food>
## <food>
## <name>Homestyle Breakfast</name>
## <price>$6.95</price>
## <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
## <calories>950</calories>
## </food>
## </breakfast_menu>
##
xL <- xmlToList(doc) ###is to convert xml doc into List
data <- ldply(xL, data.frame)
head(data)
## .id name price
## 1 food Belgian Waffles $5.95
## 2 food Strawberry Belgian Waffles $7.95
## 3 food Berry-Berry Belgian Waffles $8.95
## 4 food French Toast $4.50
## 5 food Homestyle Breakfast $6.95
## description
## 1 Two of our famous Belgian Waffles with plenty of real maple syrup
## 2 Light Belgian waffles covered with strawberries and whipped cream
## 3 Light Belgian waffles covered with an assortment of fresh berries and whipped cream
## 4 Thick slices made from our homemade sourdough bread
## 5 Two eggs, bacon or sausage, toast, and our ever-popular hash browns
## calories
## 1 650
## 2 900
## 3 900
## 4 600
## 5 950
View(data) ###to show the data present in data
write.csv(data, "xmldata.csv", row.names = FALSE) ### write.csv() - used here to store the parsed data into csv file.
getwd() ###this function will show the containing folder of csv file
## [1] "C:/Users/Satya/Desktop/jjwork/Data Science/Associate Analytics at JNTUH"
library(rjson)
doc1<-fromJSON(file="C:/Users/Satya/Desktop/sample.json", method="C")
data1 <- do.call(rbind, lapply(doc1$features, function(x) data.frame(x$properties)))
View(data1)
datageom <-do.call(rbind,lapply(doc1$features, function(x) data.frame(x$geometry)))
View(datageom)