DATA607 - Week Seven 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”).

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("XML")
## Warning: package 'XML' was built under R version 3.5.1
library("methods")

BooksXML <- xmlParse("Books.xml")

rootnode <- xmlRoot(BooksXML)
rootsize <-xmlSize(rootnode)

BooksXML_df <- xmlToDataFrame("Books.xml")
BooksXML_df
##                              Author
## 1                       Reiss, Mike
## 2                 Martin, George RR
## 3 Bernstein, Carl and Woodward, Bob
##                                                                                                  Title
## 1 Springfield Confidential, Jokes, Secrets, and Outright Lies from a Lifetime Writing for The Simpsons
## 2                                                                                    A Storm of Swords
## 3                                                                              All the President's Men
##         Genre Pages Published              ISBN
## 1      Comedy   320      2018          62748033
## 2     Fantasy   973      2000     0-553-10663-5
## 3 Non-Fiction   349      1974 978-0-671-21781-5
library(rjson)
BooksJson <- fromJSON(file="books.json", simplify = TRUE)
BooksJson_df<- as.data.frame(BooksJson)

BooksJson_df
##                              Author
## 1                       Reiss, Mike
## 2                 Martin, George RR
## 3 Bernstein, Carl and Woodward, Bob
##                                                                                                  Title
## 1 Springfield Confidential, Jokes, Secrets, and Outright Lies from a Lifetime Writing for The Simpsons
## 2                                                                                    A Storm of Swords
## 3                                                                              All the President's Men
##         Genre Pages Published              ISBN
## 1      Comedy   320      2018          62748033
## 2     Fantasy   973      2000     0-553-10663-5
## 3 Non-Fiction   349      1974 978-0-671-21781-5
BookHTML_df <- as.data.frame(readHTMLTable("Books.html"))
names(BookHTML_df) <- c("Author", "Title", "Genre", "Pages", "Published", "ISBN")
BookHTML_df 
##                              Author
## 1                       Reiss, Mike
## 2                 Martin, George RR
## 3 Bernstein, Carl and Woodward, Bob
##                                                                                                  Title
## 1 Springfield Confidential, Jokes, Secrets, and Outright Lies from a Lifetime Writing for The Simpsons
## 2                                                                                    A Storm of Swords
## 3                                                                              All the President's Men
##         Genre Pages Published              ISBN
## 1      Comedy   320      2018        0062748033
## 2     Fantasy   973      2000     0-553-10663-5
## 3 Non-Fiction   349      1974 978-0-671-21781-5

Reflections:

  1. The three data frames are nearly identical except for the BooksJson_df which imported the Pages and Published variables as numeric whereas the other two data frames imported the data as factors.

  2. After importing the HTML table, column variable was prepended with “NULL.” which I renamed.