个性化阅读
专注于IT技术分析

R XML文件操作详细图解

点击下载

本文概述

像HTML一样, XML也是一种标记语言, 代表可扩展标记语言。它是由万维网联盟(W3C)开发的, 用于定义对人类和机器均可读取的文档进行编码的语法。该文件包含标记标签。 HTML和XML之间有区别。在HTML中, 标记标记描述页面的结构, 在xml中, 标记标记描述文件中包含的数据的含义。在R中, 我们可以通过在R环境中安装” XML”包来读取xml文件。该软件包将在熟悉的命令(即install.packages)的帮助下安装。

install.packages("XML")
R XML文件

创建XML文件

我们将在给定数据的帮助下创建一个xml文件。我们将使用.xml文件扩展名保存以下数据, 以创建xml文件。 XML标签描述了数据的含义, 因此包含在此类标签中的数据可以轻松地告诉或解释该数据。

示例:xml_data.xml

<records>
<employee_info>
<id>1</id>
<name>Shubham</name>
<salary>623</salary>
<date>1/1/2012</date>
<dept>IT</dept>
</employee_info>
	
<employee_info>
<id>2</id>
<name>Nishka</name>
<salary>552</salary>
<date>1/1/2012</date>
<dept>IT</dept>
</employee_info>

<employee_info>
<id>1</id>
<name>Gunjan</name>
<salary>669</salary>
<date>1/1/2012</date>
<dept>IT</dept>
</employee_info>

<employee_info>
<id>1</id>
<name>Sumit</name>
<salary>825</salary>
<date>1/1/2012</date>
<dept>IT</dept>
</employee_info>

<employee_info>
<id>1</id>
<name>Arpita</name>
<salary>762</salary>
<date>1/1/2012</date>
<dept>IT</dept>
</employee_info>

<employee_info>
<id>1</id>
<name>Vaishali</name>
<salary>882</salary>
<date>1/1/2012</date>
<dept>IT</dept>
</employee_info>

<employee_info>
<id>1</id>
<name>Anisha</name>
<salary>783</salary>
<date>1/1/2012</date>
<dept>IT</dept>
</employee_info>

<employee_info>
<id>1</id>
<name>Ginni</name>
<salary>964</salary>
<date>1/1/2012</date>
<dept>IT</dept>
</employee_info>
	
</records>

读取XML文件

在R中, 我们可以借助xmlParse()函数轻松读取xml文件。该函数以列表形式存储在R中。要使用此函数, 我们首先需要在library()函数的帮助下加载xml包。除了xml包之外, 我们还需要加载一个名为方法的附加包。

让我们看一个示例, 以了解xmlParse()函数的工作原理, 在该示例中, 我们读取了xml_data.xml文件。

R XML文件

示例:以列表形式读取xml数据。

# Loading the package required to read XML files.
library("XML")

# Also loading the other required package.
library("methods")

# Giving the input file name to the function.
result <- xmlParse(file = "xml_data.xml")

xml_data <- xmlToList(result)
print(xml_data)

输出

R XML文件

示例:获取xml文件中存在的节点数。

# Loading the package required to read XML files.
library("XML")

# Also loading the other required package.
library("methods")

# Giving the input file name to the function.
result <- xmlParse(file = "xml_data.xml")

#Converting the data into list
xml_data <- xmlToList(result)

#Printing the data
print(xml_data)

# Exracting the root node form the xml file.
root_node <- xmlRoot(result)

# Finding the number of nodes in the root.
root_size <- xmlSize(root_node)

# Printing the result.
print(root_size)

输出

R XML文件

示例:获取xml中第一个节点的详细信息。

# Loading the package required to read XML files.
library("XML")

# Also loading the other required package.
library("methods")

# Giving the input file name to the function.
result <- xmlParse(file = "xml_data.xml")

# Exracting the root node form the xml file.
root_node <- xmlRoot(result)

# Printing the result.
print(root_node[1])

输出

R XML文件

示例:获取节点不同元素的详细信息。

# Loading the package required to read XML files.
library("XML")

# Also loading the other required package.
library("methods")

# Giving the input file name to the function.
result <- xmlParse(file = "xml_data.xml")

# Exracting the root node form the xml file.
root_node <- xmlRoot(result)

# Getting the first element of the first node.
print(root_node[[1]][[1]])

# Getting the fourth element of the first node.
print(root_node[[1]][[4]])

# Getting the third element of the third node.
print(root_node[[3]][[3]])

输出

R XML文件

如何将xml数据转换为数据框

有效地处理大文件中的数据并不容易。为此, 我们将xml文件中的数据作为数据帧读取。然后, 该数据帧由数据分析人员处理。 R提供xmlToDataFrame()函数以数据帧的形式提取信息。

让我们看一个示例, 以了解如何使用和处理此函数:

例子

# Loading the package required to read XML files.
library("XML")

# Also loading the other required package.
library("methods")

# Giving the input file name to the function xmlToDataFrame.
data_frame <- xmlToDataFrame("xml_data.xml")

#Printing the result
print(data_frame)

输出

R XML文件

赞(0)
未经允许不得转载:srcmini » R XML文件操作详细图解

评论 抢沙发

评论前必须登录!