r语言 xmlto html,R语言XML文件

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

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

install.packages("XML")

准备XML文件数据

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

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

R使用xmlParse()函数来读取xml文件,它作为列表存储在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

# 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

# Exract the root node form the xml file.

rootnode

# Find number of nodes in the root.

rootsize

# 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

# Exract the root node form the xml file.

rootnode

# 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

# Exract the root node form the xml file.

rootnode

# 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

print(xmldataframe)

当我们执行上述代码时,会产生以下结果 –

ID NAME SALARY STARTDATE DEPT

1 1 Rick 623.3 1/1/2012 IT

2 2 Dan 515.2 9/23/2013 Operations

3 3 Michelle 611 11/15/2014 IT

4 4 Ryan 729 5/11/2014 HR

5 5 Gary 843.25 3/27/2015 Finance

6 6 Nina 578 5/21/2013 IT

7 7 Simon 632.8 7/30/2013 Operations

8 8 Guru 722.5 6/17/2014 Finance

由于数据现在已经转为数据帧,所以我们可以使用数据帧相关函数来读取和操作文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值