knitr::opts_chunk$set(echo = TRUE)

Assignment 7 : 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].

library(XML)
library(DT)
library(jsonlite)
library(xml2)
library(RCurl)

I created manually an HTML, XML and JSON files that contain information about three favorite books stored in Github repositories below and can be accessible via the following links:

https://github.com/LwinShwe/607-Assignment-7/blob/main/books.html

https://github.com/LwinShwe/607-Assignment-7/blob/main/books.xml

https://github.com/LwinShwe/607-Assignment-7/blob/main/books.json

Then, load the information from these files into R data frames

# Load the html content from Github 
html <- readLines("https://raw.githubusercontent.com/LwinShwe/607-Assignment-7/main/books.html")

# Join the lines of HTML content into a single character string
html_content <- paste(html, collapse = "\n")

# Parse the HTML content into a data frame
df_html <- readHTMLTable(html_content, header = TRUE, which = 1)

# View the structure of the data frame
datatable(df_html)
# Retrieve the JSON content from Github 
json_data <- fromJSON("https://raw.githubusercontent.com/LwinShwe/607-Assignment-7/main/books.json")
# Convert the parsed JSON data into a data frame
json_df <- as.data.frame(json_data)
datatable(json_df)
xmlurl <- getURL("https://raw.githubusercontent.com/LwinShwe/607-Assignment-7/main/books.xml")
y <- xmlParse(xmlurl)
df_xml <- xmlToDataFrame(y)
datatable(df_xml)
# Load the HTML content into an R data frame
html_content <- readLines("books.html")

# Load the XML content into an R data frame
xml_content <- xmlParse("books.xml")
df_xml <- xmlToDataFrame(xml_content)

# Load the JSON content into an R data frame
json_content <- fromJSON("books.json")
df_json <- as.data.frame(json_content)
## Warning in (function (..., row.names = NULL, check.rows = FALSE, check.names =
## TRUE, : row names were found from a short variable and have been discarded
# Compare the data frames using identical
identical_html_xml <- identical(df_html, df_xml)
identical_html_json <- identical(df_html, df_json)
identical_xml_json <- identical(df_xml, df_json)

# Check the results
cat("Comparison using identical:\n")
## Comparison using identical:
cat("HTML and XML: ", identical_html_xml, "\n")
## HTML and XML:  FALSE
cat("HTML and JSON: ", identical_html_json, "\n")
## HTML and JSON:  FALSE
cat("XML and JSON: ", identical_xml_json, "\n")
## XML and JSON:  FALSE

When three of these data frame are compared in general using identical()function, the results shows that they are not totally identical.Because this function checks for column names, data types, and data values. So there are small differences in the data frames.