Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

r
xml
communitycreator

How to read xml files in R

AKASH BAJWA

XMLExtensible Markup Language is a format for representing information.

Just like HTMLHyperText Markup Language, it also supports markup tags, but unlike HTML, where the markup tag defines the structure of the page, the XML markup tags define the meaning of the data that is contained in the files.

In R, an XML file can be read using the XML package. The following command is used to install this package:

install.packages("XML")

To process XML data with R, we can include the XML library in our program by using:

library("XML")

Reading the XML file

  1. An XML file can be read in R using the function xmlParse(). Then, load data is stored in a list.
  2. An XML file can also be read in the form of a data frame by using the xmlToDataFrame() method.
main.r
employee.xml
<RECORDS>
   <EMPLOYEE>
      <ID>1</ID>
      <NAME>Jonhnny</NAME>
      <SALARY>215</SALARY>
      <STARTDATE>9/23/2015</STARTDATE>
      <DEPT>Operations</DEPT>
   </EMPLOYEE>
   
    <EMPLOYEE>
      <ID>2</ID>
      <NAME>Allen</NAME>
      <SALARY>723.3</SALARY>
      <STARTDATE>3/5/2019</STARTDATE>
      <DEPT>IT and Infrastructure</DEPT>
   </EMPLOYEE>
   
   <EMPLOYEE>
      <ID>3</ID>
      <NAME>Fench</NAME>
      <SALARY>890</SALARY>
      <STARTDATE>11/15/2014</STARTDATE>
      <DEPT>IT and Infrastructure</DEPT>
   </EMPLOYEE>
   
   <EMPLOYEE>
      <ID>4</ID>
      <NAME>Patricia</NAME>
      <SALARY>901.25</SALARY>
      <STARTDATE>6/21/2017</STARTDATE>
      <DEPT>Finance and Accounting</DEPT>
   </EMPLOYEE>

   <EMPLOYEE>
      <ID>5</ID>
      <NAME>Jennifer</NAME>
      <SALARY>780</SALARY>
      <STARTDATE>12/16/2016</STARTDATE>
      <DEPT>HR Resource Management</DEPT>
   </EMPLOYEE>
   
   <EMPLOYEE>
      <ID>6</ID>
      <NAME>George</NAME>
      <SALARY>890</SALARY>
      <STARTDATE>6/22/2018</STARTDATE>
      <DEPT>IT and Infrastructure</DEPT>
   </EMPLOYEE>
   
   <EMPLOYEE>
      <ID>7</ID>
      <NAME>Simon</NAME>
      <SALARY>1045.6</SALARY>
      <STARTDATE>7/30/2013</STARTDATE>
      <DEPT>Operations</DEPT>
   </EMPLOYEE>
   
   <EMPLOYEE>
      <ID>8</ID>
      <NAME>Crow</NAME>
      <SALARY>752.6</SALARY>
      <STARTDATE>4/12/2020</STARTDATE>
      <DEPT>Finance and Accounting</DEPT>
   </EMPLOYEE>
	
</RECORDS>
Using the xmlParse() method
Expected Output
main.r
employee.xml
# Loading the other required package
library("methods") #optional
# calling method for reading xml file
results <- xmlToDataFrame("employee.xml")
# Print as data frame.
print(results)
Using the xmlToDataFrame() method
Expected Output

RELATED TAGS

r
xml
communitycreator
RELATED COURSES

View all Courses

Keep Exploring