R XML Files Handling

XML (Extensible Markup Language) gives a standard method to access information, making it simpler for applications and devices to utilize, store, send, and show information. It is also used for many aspects, like - business-to-business transactions, e-commerce applications, generating metadata and so on. The data stores in XML are in a formatted way that can be easily rendered. That's why it is generally used in web services and data transport and is easy to search and understand. R provides 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 the library() function.

library("XML")

Reading XML File

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

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 the 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 the 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


General Knowledge



Learn Popular Language