How to Read XML in R

Sheeraz Gul Feb 02, 2024
  1. Read XML in R
  2. Read XML in a Data Frame in R
How to Read XML in R

The XML is a format to represent data; to read the XML, we must parse it first. This tutorial demonstrates how to read XML using R.

Read XML in R

To XML package in R is used to read the XML files. to use this package first, we need to install the package:

install.packages("XML")

Once the package is successfully installed, load the package and read the XML file.

Code:

library("XML")

# read the XML
result <- xmlParse(file = "delftstack.xml")

# Print the result.
print(result)

The code above will read the XML file delftstack.xml and give us the following output.

Output:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Tutorials xmlns="https://www.delftstack.com/">
    <Tutorial Id="1">
        <ArticleName>Perform String to String Array Conversion in Java</ArticleName>
        <Link>https://www.delftstack.com/howto/java/how-to-perform-string-to-string-array-conversion-in-java/</Link>
        <ProgrammingLanguage>Java</ProgrammingLanguage>
        <DateCreated>May-21, 2020</DateCreated>
    </Tutorial>
    <Tutorial Id="2">
        <ArticleName>Compile a C++ Program Using GCC</ArticleName>
        <Link>https://www.delftstack.com/howto/cpp/gcc-compile-cpp/</Link>
        <ProgrammingLanguage>C++</ProgrammingLanguage>
        <DateCreated>March-25, 2022</DateCreated>
    </Tutorial>
    <Tutorial Id="3">
        <ArticleName>Python Tutorial - Introduction</ArticleName>
        <Link>https://www.delftstack.com/tutorial/python-3-basic-tutorial/python-introduction/</Link>
        <ProgrammingLanguage>Python</ProgrammingLanguage>
        <DateCreated>January-29, 2018</DateCreated>
    </Tutorial>
</Tutorials>

Read XML in a Data Frame in R

We can also read the XML as a data frame using the methods package. First of all, install the package and load it.

install.packages("methods")
library(methods)

Now let’s read the XML into the data frame.

Code:

# read xml to data frame
result1 <- xmlToDataFrame("delftstack.xml")
# Print as data frame.
print(result1)

The code above will parse and read XML into a data frame.

Output:

                                        ArticleName
1 Perform String to String Array Conversion in Java
2                   Compile a C++ Program Using GCC
3                    Python Tutorial - Introduction
                                                                                             Link
1 https://www.delftstack.com/howto/java/how-to-perform-string-to-string-array-conversion-in-java/
2                                           https://www.delftstack.com/howto/cpp/gcc-compile-cpp/
3                https://www.delftstack.com/tutorial/python-3-basic-tutorial/python-introduction/
  ProgrammingLanguage      DateCreated
1                Java     May-21, 2020
2                 C++   March-25, 2022
3              Python January-29, 2018
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook