R语言操作XML文件实例分析
- 作者: 放浪不羁27752601
- 来源: 51数据库
- 2021-07-08
xml是一种文件格式,它使用标准ascii文本共享万维网,内部网和其他地方的文件格式和数据。 它代表可扩展标记语言(xml)。 类似于html它包含标记标签。 但是与html中的标记标记描述页面的结构不同,在xml中,标记标记描述了包含在文件中的数据的含义。
您可以使用“xml”包读取r语言中的xml文件。 此软件包可以使用以下命令安装。
install.packages("xml")
输入数据
通过将以下数据复制到文本编辑器(如记事本)中来创建xml文件。 使用.xml扩展名保存文件,并将文件类型选择为所有文件(*.*)。
<records>
<employee>
<id>1</id>
<name>rick</name>
<salary>623.3</salary>
<startdate>1/1/2012</startdate>
<dept>it</dept>
</employee>
<employee>
<id>2</id>
<name>dan</name>
<salary>515.2</salary>
<startdate>9/23/2013</startdate>
<dept>operations</dept>
</employee>
<employee>
<id>3</id>
<name>michelle</name>
<salary>611</salary>
<startdate>11/15/2014</startdate>
<dept>it</dept>
</employee>
<employee>
<id>4</id>
<name>ryan</name>
<salary>729</salary>
<startdate>5/11/2014</startdate>
<dept>hr</dept>
</employee>
<employee>
<id>5</id>
<name>gary</name>
<salary>843.25</salary>
<startdate>3/27/2015</startdate>
<dept>finance</dept>
</employee>
<employee>
<id>6</id>
<name>nina</name>
<salary>578</salary>
<startdate>5/21/2013</startdate>
<dept>it</dept>
</employee>
<employee>
<id>7</id>
<name>simon</name>
<salary>632.8</salary>
<startdate>7/30/2013</startdate>
<dept>operations</dept>
</employee>
<employee>
<id>8</id>
<name>guru</name>
<salary>722.5</salary>
<startdate>6/17/2014</startdate>
<dept>finance</dept>
</employee>
</records>
读取xml文件
xml文件由r语言使用函数xmlparse()读取。 它作为列表存储在r语言中。
# load the package required to read xml files.
library("xml")
# also load the other required package.
library("methods")
# give the input file name to the function.
result <- xmlparse(file = "input.xml")
# print the result.
print(result)
当我们执行上面的代码,它产生以下结果
1
rick
623.3
1/1/2012
it
2
dan
515.2
9/23/2013
operations
3
michelle
611
11/15/2014
it
4
ryan
729
5/11/2014
hr
5
gary
843.25
3/27/2015
finance
6
nina
578
5/21/2013
it
7
simon
632.8
7/30/2013
operations
8
guru
722.5
6/17/2014
finance
获取xml文件中存在的节点数
# load the packages required to read xml files.
library("xml")
library("methods")
# give the input file name to the function.
result <- xmlparse(file = "input.xml")
# exract the root node form the xml file.
rootnode <- xmlroot(result)
# find number of nodes in the root.
rootsize <- xmlsize(rootnode)
# print the result.
print(rootsize)
当我们执行上面的代码,它产生以下结果
output [1] 8
第一个节点的详细信息
让我们看看解析文件的第一条记录。 它将给我们一个关于存在于顶层节点中的各种元素的想法。
# load the packages required to read xml files.
library("xml")
library("methods")
# give the input file name to the function.
result <- xmlparse(file = "input.xml")
# exract the root node form the xml file.
rootnode <- xmlroot(result)
# print the result.
print(rootnode[1])
当我们执行上面的代码,它产生以下结果
$employee 1 rick 623.3 1/1/2012 it attr(,"class") [1] "xmlinternalnodelist" "xmlnodelist"
获取节点的不同元素
# load the packages required to read xml files.
library("xml")
library("methods")
# give the input file name to the function.
result <- xmlparse(file = "input.xml")
# exract the root node form the xml file.
rootnode <- xmlroot(result)
# get the first element of the first node.
print(rootnode[[1]][[1]])
# get the fifth element of the first node.
print(rootnode[[1]][[5]])
# get the second element of the third node.
print(rootnode[[3]][[2]])
当我们执行上面的代码,它产生以下结果
1 it michelle
xml到数据帧
为了在大文件中有效地处理数据,我们将xml文件中的数据作为数据框读取。 然后处理数据帧以进行数据分析。
# load the packages required to read xml files.
library("xml")
library("methods")
# convert the input xml file to a data frame.
xmldataframe <- xmltodataframe("input.xml")
print(xmldataframe)
当我们执行上面的代码,它产生以下结果
id name salary startdate dept
1 1 rick 623.30 2012-01-01 it
2 2 dan 515.20 2013-09-23 operations
3 3 michelle 611.00 2014-11-15 it
4 4 ryan 729.00 2014-05-11 hr
5 na gary 843.25 2015-03-27 finance
6 6 nina 578.00 2013-05-21 it
7 7 simon 632.80 2013-07-30 operations
8 8 guru 722.50 2014-06-17 finance
由于数据现在可以作为数据帧,我们可以使用数据帧相关函数来读取和操作文件。
到此这篇关于r语言操作xml文件实例分析的文章就介绍到这了,更多相关r语言xml文件操作内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
