R言語XMLファイル

3225 ワード

XMLは、標準ASCIIテキストを使用して万次元ネットワーク、内部ネットワーク、その他の場所のファイルフォーマットとデータを共有するファイルフォーマットです.拡張可能なタグ言語(XML)を表します.HTMLと同様にタグラベルが含まれています.ただし、HTMLのタグ記述ページとは異なり、xmlではタグ記述はファイルに含まれるデータの意味を記述する.XMLパッケージを使用して、R言語のxmlファイルを読み込むことができます.このパッケージは、次のコマンドを使用してインストールできます.install.packages(「XML」)入力データ
次のデータをメモ帳などのテキストエディタにコピーしてXMLファイルを作成します..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ファイルの読み込み
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
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[18]最初のノードの詳細
解析ファイルの最初のレコードを見てみましょう.最上位ノードに存在する様々な要素についての考え方を示します.
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])上記のコードを実行すると、$EMPLOYE 1 Rick 623.3 1/2/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 Rick 623.30 2012-01-01 IT 2 2 Dan 515.20 2013-09-23 Operations 3 Michelle 611.00 2014-11-15 IT 4 Ryan 729.00 2014-05-11 HR 5 NA Gary 843.25 2015-03-27 Finance 6 Nina 578.00 2013-05-21 IT 7 Simon 632.80 2013-07-30 Operations 8 Gu 722.50 2014-06-17 Financeデータは現在データフレームとして、データフレーム相関関数を使用してファイルを読み取り、操作できます.