Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R XML Files

R XML Files

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

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

Reading and writing XML files in R requires installing an extension package; we can enter the following command in the R console to install it:

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

Check whether it was installed successfully:

> any(grepl("XML",installed.packages()))

[1] TRUE

Create a sites.xml file in the same directory as the test script, with the following code:

Example

<

sites

<

site

<

id

1

id

<

name

Google

name

<

url

www.google.com

url

<

likes

111

likes

site

<

site

<

id

2

id

<

name

Runoob

name

<

url

www.runoob.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 data from the xml file:

Example

Load the XML package

library

(

"XML"

)

Set the file name

result

<-

xmlParse

(

file

=

"sites.xml"

)

Output 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 data amount

rootsize

<-

xmlSize

(

rootnode

)

Output result

print

(

rootsize

)

The output of the code above is:

[1] 3

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

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 item of the 2nd node

print

(

rootnode

[

[

2

]

]

[

[

1

]

]

)

View the 3rd data item of the 2nd node

print

(

rootnode

[

[

2

]

]

[

[

3

]

]

)

The output of the code above is:

$site



  2

  Runoob

  www.runoob.com

  222

 



attr(,"class")

[1] "XMLInternalNodeList" "XMLNodeList"        

2 

www.runoob.com 

Convert XML to a data list

The output of the above code is all in xml format; we can use the xmlToList() function to convert the file data into list format, which is 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 in the first row and second column

print

(

xml_data

[

[

1

]

]

[

[

2

]

]

)

The output of the code above is:

$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] "Runoob"



$site$url

[1] "www.runoob.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, making it more convenient to operate on the data:

Example

Load the XML package

library

(

"XML"

)

Convert xml file data to a data frame

xmldataframe

<-

xmlToDataFrame

(

"sites.xml"

)

print

(

xmldataframe

)

The output of the code above is:



  id   name            url likes

1  1 Google www.google.com   111

2  2 Runoob www.runoob.com   222

3  3 Taobao www.taobao.com   333