Pick three of your favorite books on one of your favorite subjects. At least one of the books should have more than one author. For each book, include the title, authors, and two or three other attributes that you find interesting.
Take the information that you’ve selected about these three books, and separately create three files which store the book’s information in HTML (using an html table), XML, and JSON formats (e.g. “books.html”,“books.xml”, and “books.json”). To help you better understand the different file structures, I’d prefer that youcreate each of these files “by hand” unless you’re already very comfortable with the file formats.
Write R code, using your packages of choice, to load the information from each of the three sources into separate R data frames. Are the three data frames identical?
library(jsonlite)
library(XML)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(RCurl)
## Loading required package: bitops
library(stringr)
All the input files(HTML,json and XML) are manually created and stored in filelocation
#books HTML
bookshtml.df1 <- readLines("./Data/books.html") %>% htmlParse() %>% readHTMLTable(which = 1)
## Warning in readLines("./Data/books.html"): incomplete final line found on
## './Data/books.html'
#Books JSON
booksjson.df2 <- fromJSON("./Data/books.json")
#Books XML
booksxml.df3 <- xmlParse("./Data/books.xml") %>% xmlToDataFrame()
Conculsion: Three dataframes where were converted from HTML, XML, JSON are unique.
#Fetching xml data from HTML page and then convert it to Json
bookshtml.link <- getURL("https://msdn.microsoft.com/en-us/library/ms762258(v=vs.85).aspx") %>% htmlParse(asText = TRUE)
bookshtml.xml <- xpathSApply(bookshtml.link,"//div[@class='codeSnippetContainerCode']",xmlValue)
booksexact <- bookshtml.xml[1] %>% str_replace_all("[\r\n]","") %>% str_trim(side = "both")
books.xml.parse <- xmlParse(booksexact) %>% xmlToDataFrame()
books.json <- toJSON(books.xml.parse)