数据:dogs.xml:
```xml
<?xml version = "1.0" encoding = "UTF-8"?>
<RECORDS>
<PEOPLE>
<ID>1</ID>
<NAME>Grey Wind</NAME>
<GENDER>Mail</GENDER>
<WEIGHT>56</WEIGHT>
</PEOPLE>
<PEOPLE>
<ID>2</ID>
<NAME>Ghost</NAME>
<GENDER>Mail</GENDER>
<WEIGHT>52</WEIGHT>
</PEOPLE>
<PEOPLE>
<ID>3</ID>
<NAME>Lady</NAME>
<GENDER>Fmail</GENDER>
<WEIGHT>46</WEIGHT>
</PEOPLE>
<PEOPLE>
<ID>4</ID>
<NAME>Nymeria</NAME>
<GENDER>Fmail</GENDER>
<WEIGHT>55</WEIGHT>
</PEOPLE>
<PEOPLE>
<ID>5</ID>
<NAME>Summer</NAME>
<GENDER>Mail</GENDER>
<WEIGHT>49</WEIGHT>
</PEOPLE>
<PEOPLE>
<ID>6</ID>
<NAME>Shaggydog</NAME>
<GENDER>Mail</GENDER>
<WEIGHT>42</WEIGHT>
</PEOPLE>
</RECORDS>
```
```R
#####转换为数据框结构
### 载入包
library("XML")
library("plyr")
### 设置工作路径并解析xml文件
setwd("E:/code/03.R/code/data/xml/example/example2")
vData <- xmlParse(file = "dogs.xml", encoding = "UTF-8")
### 使用xml包生成数据框结构
vDataFrameA <- xmlToDataFrame(vData)
print(vDataFrameA)
### 使用xml包生成列表,并使用dply包将列表转换为数据框结构
vDataList <- xmlToList(vData)
vDataFrameB <- ldply(vDataList, data.frame) #先转成list,再转dataframe
print(vDataFrameB)
##########################################################################
### 寻找根节点,转换为XMLNodeList文档
vRootNode <- xmlRoot(vData)
print(vRootNode)
### 按序号提取根节点记录
print(vRootNode[2])
### 提取单一的项
print(vRootNode[[2]][[1]])
### 查找根节点的数目(即样本总数量)
vRootSize <- xmlSize(vRootNode)
vRootSize
```
网友评论