XML which stands for Extensible Markup Language is made up of markup tags, wherein each tag illustrates the information carried by the particular attribute in the XML file. We can work with the XML files using the XML package provided by R. The package has to be explicitly installed using the following command:
Creating XML file
XML files can be created by saving the data with the respective tags containing information about the content and saving it with ‘.xml’.
We will use the following XML file ‘sample.xml’ to see the various operations that can be performed on the file:
Reading XML File
The XML file can be read after installing the package and then parsing it with
xmlparse() function, which takes as input the XML file name and prints the content of the file in the form of a list. The file should be located in the current working directory. An additional package named ‘methods’ should also be installed. The following code can be used to read the contents of the file “sample.xml”.
1 Alia 620 IT 2 Brijesh 440 Commerce 3 Yash 600 Humanities 4 Mallika 660 IT 5 Zayn 560 IT
Extracting information about the XML file
XML files can be parsed and operations can be performed on its various components. There are various in-built functions available in R, to extract the information of the nodes associated with the file, getting the number of nodes in the file, and also the specific attributes of some particular node in the file.
 number of nodes: 5  details of 2 record: $STUDENT 2 Brijesh 440 Commerce  3rd attribute of 4th record: 660
Conversion of XML to dataframe
In order to enhance the readability of the data, the XML data can be converted into a data frame consisting of a data frame comprising of rows and columns. R contains an in-built function
xmlToDataFrame() which contains as input the XML file and outputs the corresponding data in the form of a data frame. This simulates the easy handling and processing of large amounts of data.
ID NAME MARKS BRANCH 1 1 Alia 620 IT 2 2 Brijesh 440 Commerce 3 3 Yash 600 Humanities 4 4 Mallika 660 IT 5 5 Zayn 560 IT
- Working with CSV files in R Programming
- Working with Excel Files in R Programming
- Working with Binary Files in R Programming
- Working with JSON Files in R Programming
- Modify XML files with Python
- Working with Text Files in Julia
- Working with Databases in R Programming
- Data Wrangling in R Programming - Working with Tibbles
- List all the Objects present in the Current Working Directory in R Programming - ls() Function
- Reading Files in R Programming
- Reading Tabular Data from files in R Programming
- Writing to Files in R Programming
- How to convert XML file into array in PHP?
- XML Full Form
- How to generate an XML file dynamically using PHP?
- Internal working of Set in Python
- Internal working of list in Python
- Advantages and Disadvantages of working in a Startup
- Working With JSON Data in Python
- How to Get the Current Working Directory in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.