2016-04-05 16 views
0

Ich möchte einen Datenrahmen aus einer XML-Datei erstellen. Ich brauche nur bestimmte Knoten und möchte sie so anordnen, dass ich problemlos mit den Daten arbeiten kann.R & XML - Datenformat korrekt formatieren

Ich bin nur am Inhalt der verschiedenen <stroke> Knoten interessiert. Was ich brauche, sind <starttime> und <endtime>, auch <bounds> mit allen untergeordneten Knoten und allen <sample> Knoten mit <x>, <y> und <time> untergeordneten Knoten.

Dies ist mein aktueller Code:

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) 

Der interessante Teil des Datenrahmens der Strichdaten enthält wie folgt aussieht:

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.

Es sieht so aus, als ob alle Werte der Unterknoten in der Warteschlange stehen, was es schwierig macht, damit zu arbeiten. Was ist dort passiert und was kann ich tun, um das richtig zu formatieren?

Dies ist ein Überblick meiner XML-Daten:

<?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>

Antwort

1

I rvest als das beste Paket finden, um mit den XML-Dateien handelt. Unten ist keine vollständige Lösung, aber es sollte genug für Sie beginnen.

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) 

Dieser Code wird unter der Annahme, dass jede "stroke" node 1 Startzeit, Endzeit und 1 "Grenzen" node nur hat. Da es in jedem Elternteil "Strich" mehrere "Abtastknoten" gibt, muss darauf geachtet werden, dass jeder Unterknoten zum Elternteil getrennt wird. Hier wo ich anfangen würde:

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

Viel Glück.

+0

ok, danke zuerst! Ich habe das Problem verstanden und versuche es jetzt zu lösen :-) – Flugmango