#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?
# Load libraries
library(xml2)
library(data.table)
library(jsonlite)
library(tidyverse)
library(rvest)
I have created and saved all three files in HTML, xml and json in my github repository. Let’s load the files and store the info into seperate R data frames to compare the information.
html_df <- read_html("https://raw.githubusercontent.com/xhuliaturkaj/Week-7-DATA-607/main/Books.html")
html_table <- html_table(html_df, header = TRUE, fill = TRUE)[[1]]
knitr::kable(html_table)
| Title | Author | Year of Publication | Edition |
|---|---|---|---|
| Data Science in Context: Foundations, Challenges, Opportunities | Alfred Z. Spector, Peter Norvig, Chris Wiggins, Jeannette M. Wing | 2022 | New |
| Text Mining with R: A Tidy Approach | Julia Silge, David Robinson | 2017 | 1st |
| SQL for Data Analysis: Advanced Techniques for Transforming Data into Insights | Cathy Tanimura | 2021 | 1st |
Next let’s check the information from the xml file
xml <- read_xml("https://raw.githubusercontent.com/xhuliaturkaj/Week-7-DATA-607/main/Books.xml")
# Extract book elements
books <- xml %>% xml_find_all("//book")
# Create empty vectors for each column
titles <- character(0)
authors <- character(0)
years <- character(0)
editions <- character(0)
# Iterate through book elements
for (book in books) {
title <- book %>% xml_find_first(".//Title") %>% xml_text()
author_elements <- book %>% xml_find_all(".//AuthorName")
author <- paste(xml_text(author_elements), collapse = ", ")
year <- book %>% xml_find_first(".//Year") %>% xml_text()
edition <- book %>% xml_find_first(".//Edition") %>% xml_text()
# Append values to the vectors
titles <- c(titles, title)
authors <- c(authors, author)
years <- c(years, year)
editions <- c(editions, edition)
}
# Create the dataframe
books_xml_df <- data.frame(
Title = titles,
Author = authors,
Year = years,
Edition = editions
)
knitr::kable(books_xml_df)
| Title | Author | Year | Edition |
|---|---|---|---|
| Data Science in Context: Foundations, Challenges, Opportunities | Alfred Z. Spector, Peter Norvig, Chris Wiggins, Jeannette M. Wing | 2022 | New |
| Text Mining with R: A Tidy Approach | Julia Silge, David Robinson | 2017 | 1st |
| SQL for Data Analysis: Advanced Techniques for Transforming Data into Insights | Cathy Tanimura | 2021 | 1st |
Last, let’s retrieve the information from the json format file
json_table <- fromJSON("https://raw.githubusercontent.com/xhuliaturkaj/Week-7-DATA-607/main/Books.json", flatten = TRUE)
# Flatten the "Author" column and join author names with commas
json_table$Author <- sapply(json_table$Author, paste, collapse = ", ")
knitr::kable(json_table)
| Title | Author | Year of Publication | Edition |
|---|---|---|---|
| Data Science in Context: Foundations, Challenges, Opportunities | Alfred Z. Spector, Peter Norvig, Chris Wiggins, Jeannette M. Wing | 2022 | New |
| Text Mining with R: A Tidy Approach | Julia Silge, David Robinson | 2017 | 1st |
| SQL for Data Analysis: Advanced Techniques for Transforming Data into Insights | Cathy Tanimura | 2021 | 1st |
Upon retrieving the info from all three files we can confirm the information is identical, though the format is not the same.