R XML Files

XML stands for eXtensible Markup Language. XML is designed to transmit and store data.

If you are not familiar with XML, you can first refer to:XML Tutorial

To read and write XML files in R, you need to install an extension package. You can enter the following command in the R console to install it:

install.packages("XML", repos = "https://mirrors.ustc.edu.cn/CRAN/")

Check if the installation was successful:

> any(grepl("XML",installed.packages()))
[1] TRUE

Create a sites.xml file. Place the xml file in the same directory as the test script. The code is as follows:

Example

<sites> <site> <id>1</id> <name>Google</name> <url>www.google.com</url> <likes>111</likes> </site> <site> <id>2</id> <name>Example</name> <url>www.example.com</url> <likes>222</likes> </site> <site> <id>3</id> <name>Taobao</name> <url>www.taobao.com</url> <likes>333</likes> </site> </sites>

Next, we can use the XML package to load the data from the xml file:

Example

# Load the XML package
library("XML")


# Set the file name
result <- xmlParse(file = "sites.xml")

# Output the result
print(result)

Count the amount of XML data:

Example

# Load the XML package
library("XML")


# Set the file name
result <- xmlParse(file = "sites.xml")

# Extract the root node
rootnode <- xmlRoot(result)

# Count the amount of data
rootsize <- xmlSize(rootnode)

# Output the result
print(rootsize)

Executing the above code produces the following result:

[1] 3

To view node data, for a certain row use[ ], for the specified row and column use[[ ]]:

Example

# Load the XML package
library("XML")

# Set the file name
result <- xmlParse(file = "sites.xml")

# Extract the root node
rootnode <- xmlRoot(result)


# View the data of the 2nd node
print(rootnode[2])

# View the 1st data of the 2nd node
print(rootnode[[2]][[1]])

# View the 3rd data of the 2nd node

print(rootnode[[2]][[3]])

Executing the above code produces the following result:

$site
<site>
  <id>2</id>
  <name>Example</name>
  <url>www.example.com</url>
  <likes>222</likes>
</site> 

attr(,"class")
[1] "XMLInternalNodeList" "XMLNodeList"        
<id>2</id> 
<url>www.example.com</url> 

Convert XML to a Data List

The output of the above code is in XML format. We can use the xmlToList() function to convert the file data to a list format, making it more convenient to read:

Example

# Load the XML package
library("XML")


# Set the file name
result <- xmlParse(file = "sites.xml")

# Convert to a list
xml_data <- xmlToList(result)

print(xml_data)
print("============================")

# Output the data of the first row and second column
print(xml_data[[1]][[2]])

Executing the above code produces the following result:

$site
$site$id
[1] "1"

$site$name
[1] "Google"

$site$url
[1] "www.google.com"

$site$likes
[1] "111"


$site
$site$id
[1] "2"

$site$name
[1] "Example"

$site$url
[1] "www.example.com"

$site$likes
[1] "222"


$site
$site$id
[1] "3"

$site$name
[1] "Taobao"

$site$url
[1] "www.taobao.com"

$site$likes
[1] "333"


[1] "============================"
[1] "Google"

Convert XML to a Data Frame

XML file data can be converted to a data frame type, which makes it easier for us to manipulate the data:

Example

# Load the XML package
library("XML")

# Convert the XML file data to a data frame
xmldataframe <- xmlToDataFrame("sites.xml")
print(xmldataframe)

Executing the above code produces the following result:

  id   name            url likes
1  1 Google www.google.com   111
2  2 Example www.example.com   222
3  3 Taobao www.taobao.com   333
Other Extensions