欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

R語(yǔ)言操作XML文件實(shí)例分析

 更新時(shí)間:2021年05月01日 10:04:14   作者:w3cschool  
在本篇文章里小編給大家整理的是一篇關(guān)于R語(yǔ)言操作XML文件實(shí)例分析,有興趣的朋友們可以跟著學(xué)習(xí)下。

XML是一種文件格式,它使用標(biāo)準(zhǔn)ASCII文本共享萬(wàn)維網(wǎng),內(nèi)部網(wǎng)和其他地方的文件格式和數(shù)據(jù)。 它代表可擴(kuò)展標(biāo)記語(yǔ)言(XML)。 類(lèi)似于HTML它包含標(biāo)記標(biāo)簽。 但是與HTML中的標(biāo)記標(biāo)記描述頁(yè)面的結(jié)構(gòu)不同,在xml中,標(biāo)記標(biāo)記描述了包含在文件中的數(shù)據(jù)的含義。

您可以使用“XML”包讀取R語(yǔ)言中的xml文件。 此軟件包可以使用以下命令安裝。

install.packages("XML")

輸入數(shù)據(jù)

通過(guò)將以下數(shù)據(jù)復(fù)制到文本編輯器(如記事本)中來(lái)創(chuàng)建XMl文件。 使用.xml擴(kuò)展名保存文件,并將文件類(lèi)型選擇為所有文件(*.*)。

<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語(yǔ)言使用函數(shù)xmlParse()讀取。 它作為列表存儲(chǔ)在R語(yǔ)言中。

# 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)

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

    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文件中存在的節(jié)點(diǎn)數(shù)

# 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)

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

output
[1] 8

第一個(gè)節(jié)點(diǎn)的詳細(xì)信息

讓我們看看解析文件的第一條記錄。 它將給我們一個(gè)關(guān)于存在于頂層節(jié)點(diǎn)中的各種元素的想法。

# 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])

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

$EMPLOYEE
  1
  Rick
  623.3
  1/1/2012
  IT
 

attr(,"class")
[1] "XMLInternalNodeList" "XMLNodeList" 

獲取節(jié)點(diǎn)的不同元素

# 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]])

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

1 
IT 
Michelle 

XML到數(shù)據(jù)幀

為了在大文件中有效地處理數(shù)據(jù),我們將xml文件中的數(shù)據(jù)作為數(shù)據(jù)框讀取。 然后處理數(shù)據(jù)幀以進(jìn)行數(shù)據(jù)分析。

# 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)

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

      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

由于數(shù)據(jù)現(xiàn)在可以作為數(shù)據(jù)幀,我們可以使用數(shù)據(jù)幀相關(guān)函數(shù)來(lái)讀取和操作文件。

到此這篇關(guān)于R語(yǔ)言操作XML文件實(shí)例分析的文章就介紹到這了,更多相關(guān)R語(yǔ)言XML文件操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論