Assignment - Working with XML and JSON in R

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 you create 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?

Your deliverable is the three source files and the R code. If you can, package your assignment solution up into an .Rmd file and publish to rpubs.com. [This will also require finding a way to make your three text files accessible from the web].

Getting Data from url

library(RCurl)
## Warning: package 'RCurl' was built under R version 3.5.1
## Loading required package: bitops
url_html <- "https://raw.githubusercontent.com/maharjansudhan/DATA607/master/book.html"
url_json <- "https://raw.githubusercontent.com/maharjansudhan/DATA607/master/book.json"
url_xml <- "https://raw.githubusercontent.com/maharjansudhan/DATA607/master/book.xml"


Lhtml <- getURL(url_html)
Ljson <- getURL(url_json)
Lxml <- getURL(url_xml)

Now viewing the information from the particular data.

## display the josn file information

library(RJSONIO)
jsonL <- fromJSON(Ljson)

jsonL
## $fbooks
## $fbooks[[1]]
## $fbooks[[1]]$title
## [1] "The Talisman"
## 
## $fbooks[[1]]$authors
## [1] "Stephen King" "Peter Straub"
## 
## $fbooks[[1]]$publisher
## [1] "Random House"
## 
## $fbooks[[1]]$yearpublished
## [1] 2001
## 
## 
## $fbooks[[2]]
## $fbooks[[2]]$title
## [1] "Macbeth"
## 
## $fbooks[[2]]$authors
## [1] "William Shakespare"
## 
## $fbooks[[2]]$publisher
## [1] "Simon Schuster"
## 
## $fbooks[[2]]$yearpublished
## [1] 2003
## 
## 
## $fbooks[[3]]
## $fbooks[[3]]$title
## [1] "A Tale of Two Cities"
## 
## $fbooks[[3]]$authors
## [1] "Charles Dickens"
## 
## $fbooks[[3]]$publisher
## [1] "Penguin"
## 
## $fbooks[[3]]$yearpublished
## [1] 2003
## display the xml file information

library(XML)
## Warning: package 'XML' was built under R version 3.5.1
xmlh <- xmlParse(Lxml)
xmldf <- xmlToDataFrame(xmlh)
xmldf
##                  title                 authorname      publisher
## 1         The Talisman Stephen King, Peter Straub   Random House
## 2              Macbeth         William Shakespare Simon Schuster
## 3 A Tale of Two Cities            Charles Dickens        Penguin
##   yearpublished
## 1          2001
## 2          2003
## 3          2003
## display the html file information

table <- readHTMLTable(Lhtml, header = TRUE)
htmldf <- data.frame(table )
colnames(htmldf) <- c("Title","Authors","Publisher","YearPublished")
htmldf
##                  Title                    Authors      Publisher
## 1       The Talisman R Stephen King, Peter Straub   Random House
## 2              Macbeth         William Shakespare Simon Schuster
## 3 A Tale of Two Cities            Charles Dickens        Penguin
##   YearPublished
## 1          2001
## 2          2003
## 3          2003

XML and HTML displays as a list. These displays the similar kind of data.

JSON displays as a data frame.