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?

Solution
The files were created in Notepad++ first starting with json array and then the others.

1. books.xml

XmlB <- xmlParse(read_xml('https://raw.githubusercontent.com/henryvalentine/MSDS2019/master/Classes/DATA%20607/Home%20works/Week7/books.xml'))
bXml <- xmlRoot(XmlB)
booksData <- xmlSApply(bXml,function(x) xmlSApply(x, xmlValue))
booksXml <- data.frame(t(booksData),row.names=NULL)
datatable(booksXml, colnames= c("Author(s)", "Date Publish", "Genre", "Number of Reviewers", "Pages", "Rating", "Title", "Type"), class = 'cell-border stripe', options = list(
  initComplete = JS(
    "function(settings, json) {",
    "$(this.api().table().header()).css({'background-color': '#26868d', 'color': '#fff', 'text-align': 'center !important'});",
    "$(this.api().table().body()).css({'color': '#000', 'text-align': 'center !important'});",
    "}")
))

books.json

booksJson <- read_json("https://raw.githubusercontent.com/henryvalentine/MSDS2019/master/Classes/DATA%20607/Home%20works/Week7/books.json", simplifyVector = TRUE)
datatable(booksJson, colnames= c("Title", "Type", "Author(s)", "Genre", "Date Publish", "Rating", "Pages", "Number of Reviewers"), class = 'cell-border stripe', options = list(
  initComplete = JS(
    "function(settings, json) {",
    "$(this.api().table().header()).css({'background-color': '#26868d', 'color': '#fff', 'text-align': 'center !important'});",
    "$(this.api().table().body()).css({'color': '#000', 'text-align': 'center !important'});",
    "}")
))

books.html

booksHtml<- as.data.frame(html_nodes(read_html('https://raw.githubusercontent.com/henryvalentine/MSDS2019/master/Classes/DATA%20607/Home%20works/Week7/books.html'), "table")%>% html_table(fill=TRUE))
datatable(booksHtml, colnames= c("Title", "Type", "Author(s)", "Genre", "Date Publish", "Rating", "Pages", "Number of Reviewers"), class = 'cell-border stripe', options = list(
  initComplete = JS(
    "function(settings, json) {",
    "$(this.api().table().header()).css({'background-color': '#26868d', 'color': '#fff', 'text-align': 'center !important'});",
    "$(this.api().table().body()).css({'color': '#000', 'text-align': 'center !important'});",
    "}")
))
This needs to be cleaned up as it contains empty spaces
htmlBooks <- booksHtml%>% dplyr::filter(!(title==""))

datatable(htmlBooks, colnames= c("Title", "Type", "Author(s)", "Genre", "Date Publish", "Rating", "Pages", "Number of Reviewers"), class = 'cell-border stripe', options = list(
  initComplete = JS(
    "function(settings, json) {",
    "$(this.api().table().header()).css({'background-color': '#26868d', 'color': '#fff', 'text-align': 'center !important'});",
    "$(this.api().table().body()).css({'color': '#000', 'text-align': 'center !important'});",
    "}")
))
The dataframes are similar except that reading from the xml file reordered the column headers alphabetically. The Html file has some trailing spaces which had to be cleaned up.