DEV Community

loading...

R Language : Read XML files In R

maheshkay profile image Mahesh K Updated on ・1 min read

Let's discuss how to Read XML Files in R. There are many places where people make use of the XML data. Often some specific set of data is being stored into the XML.

Prior to JSON files getting popular as they are today, many companies used to offer the XML export option. So you are going to find many legacy files that store the data in XML file. So we are going to see how to read this data with the R language.

You can watch the video instruction of the tutorial - How to Read XML Files in R.

XML Package

In order to read the XML file and to manipulate the data, you need to make use of the XML package. You can install the package with the following code.

install.packages("XML")

So let's assume we have XML file named mydata.xml. You can get the XML data from sample file.

Now we can get started to import the package.

library("XML")

Next package that we would be needing to be added is the following.

library("methods")

Let's name the input file and import the file.

dt <- xmlParse(file = "mydata.xml")

So here's the data variable named "dt". You can print the output as follows.

print(dt)

Here's the output that you'd get in the console.

<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<calories>650</calories>
</food>
</breakfast_menu>

I hope this post helps you to read the XML data from the file in R language.

Discussion

pic
Editor guide
Collapse
mccurcio profile image
Matt Curcio

Good start, but how does one get the specific information? How do you choose the info would be good to add too. :))
Thanks