Gehad Gad

March 15th, 2020

DATA 607 Assignment 7

Assignment instructions: Pick three of you 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.

#Import libraries and/or Packages

library(XML)
## Warning: package 'XML' was built under R version 3.6.2
library(RCurl)
## Warning: package 'RCurl' was built under R version 3.6.2
library(rjson)
library(jsonlite)
## 
## Attaching package: 'jsonlite'
## The following objects are masked from 'package:rjson':
## 
##     fromJSON, toJSON
library(DT)
## Warning: package 'DT' was built under R version 3.6.3

#Read HTML file

#I created a booklist and uploaded it to be read from GitHub. 

Booklist_html <-"https://raw.githubusercontent.com/GehadGad/DATA607-Assignment-7-html/master/Booklist.html"

Booklist_html <- getURL (Booklist_html)

Booklist_html <- readHTMLTable(Booklist_html)

BookList1 <- Booklist_html[[1]]

datatable(BookList1)
str(BookList1)
## 'data.frame':    3 obs. of  5 variables:
##  $ Title    : Factor w/ 3 levels "Awaken the Giant Within",..: 1 3 2
##  $ Author   : Factor w/ 3 levels "Adele Faber, Elaine Mazlin",..: 3 2 1
##  $ ISBN     : Factor w/ 3 levels "0671791540","9780743247542",..: 1 2 3
##  $ Publisher: Factor w/ 3 levels "Scribner; Reprint edition (January 17, 2006)",..: 3 1 2
##  $ Pages    : Factor w/ 3 levels "288 pages","370 pages",..: 3 1 2

#Read JSON file

Booklist_json <- "https://raw.githubusercontent.com/GehadGad/DAT-607-Assignment-7-Json/master/Booklist.json"

Book_Json <- getURLContent(Booklist_json)
booksJ= fromJSON(Book_Json)
booksJSON <- data.frame(booksJ)
datatable(booksJSON)
str(booksJSON)
## 'data.frame':    3 obs. of  5 variables:
##  $ Booklist.Title    : chr  "Awaken the Giant Within" "The Glass Castle" "How to Talk So Kids Will Listen & Listen So Kids Will Talk"
##  $ Booklist.Author   :List of 3
##   ..$ : chr "TonY Robbins"
##   ..$ : chr "Jeannette Walls"
##   ..$ : chr "Adele Faber, Elaine Mazlin"
##  $ Booklist.ISBN     : chr  "0671791540" "9780743247542" "B005GG0MXI"
##  $ Booklist.Publisher: chr  "Simon and Schuster (November 1, 1992)" "Scribner; Reprint edition (January 17, 2006)" "Scribner; Updated edition (February 7, 2012)"
##  $ Booklist.Pages    : int  544 288 370

#Read XML file

Booklist_xml<- "https://raw.githubusercontent.com/GehadGad/DATA-607-Assignment-7-Book.xml/master/Booklist3.xml"

Booklist <- getURL(Booklist_xml)

BooklistXML <- xmlTreeParse(Booklist)

class(BooklistXML)
## [1] "XMLDocument"         "XMLAbstractDocument"
topxml <- xmlRoot(BooklistXML)
topxml <- xmlSApply(topxml,
function(x) xmlSApply(x, xmlValue))

xml_df <- data.frame(t(topxml),
                     row.names=NULL)

datatable (xml_df)

Conclusion:

HTML and XML are very similar. JSON is a little different.