2016-04-05 8 views
0

XMLファイルからデータフレームを作成したい。私は特定のノードしか必要とせず、簡単にデータを扱うことができる方法でそれらを整理したいと思う。R&XML - データフレームを正しくフォーマットする

私は異なる<stroke>ノードの内容にのみ興味があります。 私が必要としているのは<starttime><endtime>です。また、すべてのサブノードを持つ<bounds><x><y><time>というサブノードを持つすべての<sample>ノードです。

これは私の現在のコードです:

UnassignedStrokes 1 1459867893629, 1459867896812, 145986789362914598678948151.0-14090101260.0750.0217.018.0260.625766.01459867893629108260.625763.6251459867893722120262.875762.01459867893775122278.0757.1251459867893935124294.875755.751459867894015124304.875755.1251459867894055124319.125755.3751459867894109124326.75754.751459867894135124355.0756.51459867894229124372.375756.3751459867894282124388.625755.3751459867894335124401.375756.1251459867894375124427.75754.3751459867894469124448.75752.1251459867894549124455.5750.8751459867894575124473.75751.251459867894669124476.125752.01459867894789124474.25751.751459867894802118469.875750.7514598678948150, 145986789589214598678968121.0-14090101364.0701.010.0125.0364.5701.0145986789589232366.0702.01459867895905106367.25702.6251459867895958120367.5703.1251459867896012122369.25724.1251459867896358126369.375748.01459867896465126370.125765.01459867896532126369.875779.51459867896598126369.125789.1251459867896638126369.375795.251459867896665126370.875803.751459867896705126373.125825.6251459867896812126

file <- "1.xml" 
xmlfile <- xmlTreeParse(file) 
xmltop <- xmlRoot(xmlfile) 
values <- xmlSApply(xmltop, function(x) xmlSApply(x, xmlValue)) 
df <- data.frame(t(values),row.names=NULL) 

ストロークデータを含むデータフレームの興味深い部分はこのようになります。

サブノードのすべての値がキューに入れられているため、そのサブノードで処理するのが難しいようです。そこに何が起こったのですか?これを正しくフォーマットするために何ができますか?

これは私のXMLデータのスナップショットです:

<?xml version="1.0" encoding="UTF-8" ?> 
 
<page> 
 
    <id>1459867896812</id> 
 
    <bookId>1459867896812</bookId> 
 
    <paperspace>21.49.16.203</paperspace> 
 
    <checkDate>2016-04-05 16:51:36.812</checkDate> 
 
    <penCheckDate>2016-04-05 16:51:36.812</penCheckDate> 
 
    <receiveDate>2016-04-05 16:51:48.363</receiveDate> 
 
    <pageNr>203</pageNr> 
 
    <pages>1</pages> 
 
    <processStatus>received</processStatus> 
 
    <penData> 
 
    <expiration>0</expiration> 
 
    <signature/> 
 
    <toEnterprise>1</toEnterprise> 
 
    <company>Universitaet Muenster</company> 
 
    <mandator>Uni_Muenster</mandator> 
 
    <comment>Universitaet Muenster</comment> 
 
    <pagesToComplete/> 
 
    <previousPages>0</previousPages> 
 
    <clientAttribute name="vertrag">XXXX</clientAttribute> 
 
    </penData> 
 
    <bounds> 
 
    <x>260.0</x> 
 
    <y>701.0</y> 
 
    <width>217.0</width> 
 
    <height>125.0</height> 
 
    </bounds> 
 
    <UnassignedStrokes> 
 
    <starttime>1459867893629</starttime> 
 
    <endtime>1459867896812</endtime> 
 
    <stroke> 
 
     <starttime>1459867893629</starttime> 
 
     <endtime>1459867894815</endtime> 
 
     <linewidth>1.0</linewidth> 
 
     <color>-14090101</color> 
 
     <bounds> 
 
     <x>260.0</x> 
 
     <y>750.0</y> 
 
     <width>217.0</width> 
 
     <height>18.0</height> 
 
     </bounds> 
 
     <sample> 
 
     <x>260.625</x> 
 
     <y>766.0</y> 
 
     <time>1459867893629</time> 
 
     <force>108</force> 
 
     </sample> 
 
     <sample> 
 
     <x>260.625</x> 
 
     <y>763.625</y> 
 
     <time>1459867893722</time> 
 
     <force>120</force> 
 
     </sample> 
 
     <sample> 
 
     <x>262.875</x> 
 
     <y>762.0</y> 
 
     <time>1459867893775</time> 
 
     <force>122</force> 
 
     </sample> 
 
     <sample> 
 
     <x>278.0</x> 
 
     <y>757.125</y> 
 
     <time>1459867893935</time> 
 
     <force>124</force> 
 
     </sample> 
 
     <sample> 
 
     <x>294.875</x> 
 
     <y>755.75</y> 
 
     <time>1459867894015</time> 
 
     <force>124</force> 
 
     </sample> 
 
     <sample> 
 
     <x>304.875</x> 
 
     <y>755.125</y> 
 
     <time>1459867894055</time> 
 
     <force>124</force> 
 
     </sample> 
 
     <sample> 
 
     <x>319.125</x> 
 
     <y>755.375</y> 
 
     <time>1459867894109</time> 
 
     <force>124</force> 
 
     </sample> 
 
     <sample> 
 
     <x>474.25</x> 
 
     <y>751.75</y> 
 
     <time>1459867894802</time> 
 
     <force>118</force> 
 
     </sample> 
 
     <sample> 
 
     <x>469.875</x> 
 
     <y>750.75</y> 
 
     <time>1459867894815</time> 
 
     <force>0</force> 
 
     </sample> 
 
    </stroke> 
 
    <stroke> 
 
     <starttime>1459867895892</starttime> 
 
     <endtime>1459867896812</endtime> 
 
     <linewidth>1.0</linewidth> 
 
     <color>-14090101</color> 
 
     <bounds> 
 
     <x>364.0</x> 
 
     <y>701.0</y> 
 
     <width>10.0</width> 
 
     <height>125.0</height> 
 
     </bounds> 
 
     <sample> 
 
     <x>364.5</x> 
 
     <y>701.0</y> 
 
     <time>1459867895892</time> 
 
     <force>32</force> 
 
     </sample> 
 
     <sample> 
 
     <x>366.0</x> 
 
     <y>702.0</y> 
 
     <time>1459867895905</time> 
 
     <force>106</force> 
 
     </sample> 
 
     <sample> 
 
     <x>367.25</x> 
 
     <y>702.625</y> 
 
     <time>1459867895958</time> 
 
     <force>120</force> 
 
     </sample> 
 
     <sample> 
 
     <x>367.5</x> 
 
     <y>703.125</y> 
 
     <time>1459867896012</time> 
 
     <force>122</force> 
 
     </sample> 
 
     <sample> 
 
     <x>369.25</x> 
 
     <y>724.125</y> 
 
     <time>1459867896358</time> 
 
     <force>126</force> 
 
     </sample> 
 
     <sample> 
 
     <x>369.375</x> 
 
     <y>748.0</y> 
 
     <time>1459867896465</time> 
 
     <force>126</force> 
 
     </sample> 
 
     <sample> 
 
     <x>370.125</x> 
 
     <y>765.0</y> 
 
     <time>1459867896532</time> 
 
     <force>126</force> 
 
     </sample> 
 
     <sample> 
 
     <x>369.875</x> 
 
     <y>779.5</y> 
 
     <time>1459867896598</time> 
 
     <force>126</force> 
 
     </sample> 
 
     <sample> 
 
     <x>369.125</x> 
 
     <y>789.125</y> 
 
     <time>1459867896638</time> 
 
     <force>126</force> 
 
     </sample> 
 
     <sample> 
 
     <x>369.375</x> 
 
     <y>795.25</y> 
 
     <time>1459867896665</time> 
 
     <force>126</force> 
 
     </sample> 
 
     <sample> 
 
     <x>370.875</x> 
 
     <y>803.75</y> 
 
     <time>1459867896705</time> 
 
     <force>126</force> 
 
     </sample> 
 
     <sample> 
 
     <x>373.125</x> 
 
     <y>825.625</y> 
 
     <time>1459867896812</time> 
 
     <force>126</force> 
 
     </sample> 
 
    </stroke> 
 
    </UnassignedStrokes> 
 
</page>

答えて

1

私はxmlファイルを扱うための最良のパッケージとしてrvest見つけます。以下は完全な解決策ではありませんが、開始するには十分なはずです。

library(rvest) 

myxml<-read_xml("text.xml") 
#find the only the stroke nodes 
stroke<-xml_nodes(myxml,"stroke") 
#extract the start and end times 
starttime<-xml_text(xml_nodes(stroke, "starttime")) 
endtime<-xml_text(xml_nodes(stroke, "endtime")) 
#find the bounds nodes under each stroke node 
bounds<-xml_nodes(stroke, "bounds") 
#extract out the x, y width and heigth from each bound node 
x<-xml_text(xml_nodes(bounds, "x")) 
y<-xml_text(xml_nodes(bounds, "y")) 
width<-xml_text(xml_nodes(bounds, "width")) 
height<-xml_text(xml_nodes(bounds, "height")) 

#save to dataframe 
df<-data.frame(starttime, endtime, x, y, width, height) 
このコードでは、各「ストローク」ノードには1つの開始時刻、終了時刻、1つの「境界」ノードしかないものと想定しています。各親「ストローク」には複数の「サンプル」ノードが存在するため、各サブノードを親ノードに分割する必要があります。ここで私が始めるところ:

samples<-sapply(stroke, FUN=xml_nodes, xpath="sample") 

幸運。

+0

よろしくお願いします。私は問題を理解し、今それを解決しようとします:-) – Flugmango

関連する問題