R语言操作XML文件实例分析

在本篇文章里小编给大家整理的是一篇关于R语言操作XML文件实例分析,有兴趣的朋友们可以跟着学习下。

XML是一种文件格式,它使用标准ASCII文本共享万维网,内部网和其他地方的文件格式和数据。 它代表可扩展标记语言(XML)。 类似于HTML它包含标记标签。 但是与HTML中的标记标记描述页面的结构不同,在xml中,标记标记描述了包含在文件中的数据的含义。

您可以使用“XML”包读取R语言中的xml文件。 此软件包可以使用以下命令安装。

 install.packages("XML") 

输入数据

通过将以下数据复制到文本编辑器(如记事本)中来创建XMl文件。 使用.xml扩展名保存文件,并将文件类型选择为所有文件(*.*)。

   1Rick623.31/1/2012IT 2Dan515.29/23/2013Operations 3Michelle61111/15/2014IT 4Ryan7295/11/2014HR 5Gary843.253/27/2015Finance 6Nina5785/21/2013IT 7Simon632.87/30/2013Operations 8Guru722.56/17/2014Finance

读取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文件实例分析的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » R语言