Leer XML en R

Sheeraz Gul 21 junio 2023
  1. Leer XML en R
  2. Leer XML en un marco de datos en R
Leer XML en R

El XML es un formato para representar datos; para leer el XML, primero debemos analizarlo. Este tutorial demuestra cómo leer XML usando R.

Leer XML en R

El paquete XML en R se utiliza para leer los archivos XML. para usar este paquete primero, necesitamos instalar el paquete:

install.packages("XML")

Una vez que el paquete se haya instalado correctamente, cárguelo y lea el archivo XML.

Código:

library("XML")

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

# Print the result.
print(result)

El código anterior leerá el archivo XML delftstack.xml y nos dará el siguiente resultado.

Producción :

<?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>

Leer XML en un marco de datos en R

También podemos leer el XML como un marco de datos utilizando el paquete métodos. En primer lugar, instale el paquete y cárguelo.

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

Ahora leamos el XML en el marco de datos.

Código:

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

El código anterior analizará y leerá XML en un marco de datos.

Producción :

                                        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
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