Angel Gallardo

load Data table from HTML, XML, and JSON

open html table as a dataframe

library(rvest)
## Warning: package 'rvest' was built under R version 4.3.3
#read HTML
url <- "https://raw.githubusercontent.com/Angelogallardo05/DATA607-wk7/main/booksinfo.html"


html <- read_html(url)


table <- html_table(html)

# Access the first (and only) table
df <- table[[1]]


print(df)
## # A tibble: 3 × 4
##   Title                                   Authors         Genre `Year published`
##   <chr>                                   <chr>           <chr> <chr>           
## 1 How to Win Friends and Influence People Dale Carnegie   Self… October 1936    
## 2 Corporate Finance 11th edition          Stephen Ross, … Busi… October 2015    
## 3 The Intelligent Investor                Benjamin Graham Busi… February 2006

#Open xml table as a dataframe

library(xml2)
library(magrittr)

xml_file <- "https://raw.githubusercontent.com/Angelogallardo05/DATA607-wk7/main/booksxml.xml"
doc <- read_xml(xml_file)

# Extract data from XML nodes
titles <- xml_text(xml_find_all(doc, "//title"))
authors <- xml_text(xml_find_all(doc, "//authors"))
genres <- xml_text(xml_find_all(doc, "//genre"))
years_published <- xml_text(xml_find_all(doc, "//year_published"))

# Create a data frame
books_df <- data.frame(
  Title = titles,
  Authors = authors,
  Genre = genres,
  Year_Published = years_published
)


print(books_df)
##                                     Title
## 1 How to Win Friends and Influence People
## 2          Corporate Finance 11th edition
## 3                The Intelligent Investor
##                                                               Authors
## 1                                                       Dale Carnegie
## 2 Stephen Ross, Randal W. Westerfield, Jeffrey Jaffe, Bradford Jordan
## 3                                                     Benjamin Graham
##                Genre Year_Published
## 1          Self-help   October 1936
## 2 Business/Economics   October 2015
## 3   Business & Money  February 2006

#open Json file as a datraframe

library(jsonlite)
## Warning: package 'jsonlite' was built under R version 4.3.3
# Read the JSON file
json_file <- "https://raw.githubusercontent.com/Angelogallardo05/DATA607-wk7/main/booksinfo.json"
json_data <- fromJSON(json_file)

# Extract the book data
books_df <- json_data$library$books


print(books_df)
##                                     title
## 1 How to Win Friends and Influence People
## 2          Corporate Finance 11th edition
## 3                The Intelligent Investor
##                                                               authors
## 1                                                       Dale Carnegie
## 2 Stephen Ross, Randal W. Westerfield, Jeffrey Jaffe, Bradford Jordan
## 3                                                     Benjamin Graham
##                genre year_published
## 1          Self-help   October 1936
## 2 Business/Economics   October 2015
## 3   Business & Money  February 2006

all loaded the same