R XML Files Handling

XML is a good web service for transporting data. This is a great source to share data in the world wide web. R provide some packages to perform actions in XML.

So, what we need first is to install the package.

Install XML Package

To install a package, use the install.package() command with the package name, like as follows -

install.packages("XML")

Load XML Package

We hope you have successfully installed the XML package. Now to use this package in your application, you need to load the package library by using library() function.

library("XML")

Reading XML File

R provides xmlParse() function to read the data from an xml file. The syntax of xmlParse() is -

xmlParse(file = "xmlfilename")




Example of R XML Files Handling

Suppose, an xml file name 'employee.xml' located in the current working directory.

<COMPANYNAME>
<EMPLOYEE>
	<ID>1</ID>
	<NAME>Dhyan</NAME>
	<AGE>26</AGE>
	<DEPARTMENT>HR</DEPARTMENT>
</EMPLOYEE>
<EMPLOYEE>
	<ID>2</ID>
	<NAME>Jorz</NAME>
	<AGE>27</AGE>
	<DEPARTMENT>Finance</DEPARTMENT>
</EMPLOYEE>
<EMPLOYEE>
	<ID>3</ID>
	<NAME>Mary</NAME>
	<AGE>29</AGE>
	<DEPARTMENT>IT</DEPARTMENT>
</EMPLOYEE>
<EMPLOYEE>
	<ID>4</ID>
	<NAME>Sinoy</NAME>
	<AGE>32</AGE>
	<DEPARTMENT>IT</DEPARTMENT>
</EMPLOYEE>
</COMPANYNAME>

The following command reads the above xml file -

xmldata <- xmlParse(file = 'employee.xml')
print(xmldata)

R XML



XML to Data Frame

We can also convert the xml file data to data frame by using xmlToDataFrame() function.

data <- xmlToDataFrame('employee.xml')
print(data)
  ID  NAME AGE DEPARTMENT
1  1 Dhyan  26         HR
2  2  Jorz  27    Finance
3  3  Mary  29         IT
4  4 Sinoy  32         IT

Write Data Frame to XML

We can also convert the xml file data to data frame by using xmlToDataFrame() function.

students <- data.frame(id = c(1:4), name = c("Mary", "Soy", "Alexa", "Roxy"), class = 5)

xml <- xmlTree()
xml$addTag("document", close=FALSE)
for (i in 1:nrow(students)) {
    xml$addTag("row", close=FALSE)
    for (j in names(students)) {
        xml$addTag(j, students[i, j])
    }
    xml$closeTag()
}
xml$closeTag()

# display the result
cat(saveXML(xml))

<?xml version="1.0"?>
<document>
<row>
<id>1</id>
<name>Mary</name>
<class>5</class>
</row>
<row>
<id>2</id>
<name>Soy</name>
<class>5</class>
</row>
<row>
<id>3</id>
<name>Alexa</name>
<class>5</class>
</row>
<row>
<id>4</id>
<name>Roxy</name>
<class>5</class>
</row>
</document>






Read more articles