Home » R XML File

R XML File

Like HTML, XML is also a markup language which stands for Extensible Markup Language. It is developed by World Wide Web Consortium(W3C) to define the syntax for encoding documents which both humans and machine can read. This file contains markup tags. There is a difference between HTML and XML. In HTML, the markup tag describes the structure of the page, and in xml, it describes the meaning of the data contained in the file. In R, we can read the xml files by installing “XML” package into the R environment. This package will be installed with the help of the familiar command i.e., install.packages.

R XML File

Creating XML File

We will create an xml file with the help of the given data. We will save the following data with the .xml file extension to create an xml file. XML tags describe the meaning of data, so that data contained in such tags can easily tell or explain about the data.

Example: xml_data.xml

Reading XML File

In R, we can easily read an xml file with the help of xmlParse() function. This function is stored as a list in R. To use this function, we first need to load the xml package with the help of the library() function. Apart from the xml package, we also need to load one additional package named methods.

Let’s see an example to understand the working of xmlParse() function in which we read our xml_data.xml file.

R XML File

Example: Reading xml data in the form of a list.

Output

R XML File

Example: Getting number of nodes present in xml file.

Output

R XML File

Example: Getting details of the first node in xml.

Output

R XML File

Example: Getting details of different elements of a node.

Output

R XML File

How to convert xml data into a data frame

It’s not easy to handle data effectively in large files. For this purpose, we read the data in the xml file as a data frame. Then this data frame is processed by the data analyst. R provide xmlToDataFrame() function to extract the information in the form of Data Frame.

Let’s see an example to understand how this function is used and processed:

Example

Output

R XML File


Next TopicR Database

You may also like