2016-05-31 6 views
0

Ich bin ziemlich neu in R und versuche, eine XML-Datei zu lesen und in XP mit einem Datenrahmen in R umzuwandeln. Ich habe eine Lösung gefunden, die die Datei in eine Liste verwandelt, von wo aus ich in der Lage wäre, damit umzugehen. Allerdings muss mein Programm relativ schnell laufen. Ich habe das Tutorial auf w3school.com überprüft (http://www.w3schools.com/xsl/xpath_nodes.asp)on XPath, aber sie erklären nicht die Notation, die ich in meiner XML-Datei finden. Ich möchte einen Datenrahmen mit den verschiedenen Kunden und ihre Attribute. Der Anfang der Datei ist nicht in meinen Berechnungen benötigtXML-Knoten-Notation (XPath zu Dataframe in R)

nach einem Auszug aus der Datei ist:.?

$config 
<config> 
    <competition id="0" name="0" pomId="1.3.1-SNAPSHOT" timeslotLength="60" bootstrapTimeslotCount="336" bootstrapDiscardedTimeslots="24" timeslotsOpen="24" deactivateTimeslotsAhead="1" minimumOrderQuantity="0.01" timezoneOffset="-6" latitude="45" simulationRate="720" simulationModulo="3600000"> 
<description/> 
<simulationBaseTime> 
    <iMillis>1255132800000</iMillis> 
</simulationBaseTime> 
<broker>default broker</broker> 
<customer id="4097" name="HighIncome-2_8" population="1" powerType="ELECTRIC_VEHICLE" customerClass="SMALL" controllableKW="-3.3" upRegulationKW="-3.3" downRegulationKW="3.3" storageCapacity="85.0" multiContracting="false" canNegotiate="false"/> 
<customer id="4100" name="HighIncome-2_9" population="1" powerType="ELECTRIC_VEHICLE" customerClass="SMALL" controllableKW="-3.3" upRegulationKW="-3.3" downRegulationKW="3.3" storageCapacity="60.0" multiContracting="false" canNegotiate="false"/> 
<customer id="4103" name="HighIncome-2_10" population="1" powerType="ELECTRIC_VEHICLE" customerClass="SMALL" controllableKW="-3.3" upRegulationKW="-3.3" downRegulationKW="3.3" storageCapacity="60.0" multiContracting="false" canNegotiate="false"/> 
<customer id="4106" name="HighIncome-2_11" population="1" powerType="ELECTRIC_VEHICLE" customerClass="SMALL" controllableKW="-3.3" upRegulationKW="-3.3" downRegulationKW="3.3" storageCapacity="85.0" multiContracting="false" canNegotiate="false"/> 

Wie verweise ich auf jeden Kunden sind sie ein Attributknoten, ein Attribut

Antwort

0

in XML? Es gibt zwei Strukturtypen, die Werte enthalten:

  1. Element (auch als Knoten oder tag bezeichnet), umschlossen mit eckigen Klammern, deren Wert zwischen der Öffnung <element> und Schließen </element>
  2. Attribut mit dem Präfix @ dessen Wert zugeordnet gleich Bediener
gehalten wird

Für Ihre spezifischen XML, Kunde ist das Element mit id, Name, Bevölkerung, power, customerClass, controllableKW, upRegulationKW, downRegulationKW, storageCapacity, multiContracting und canNegotiate als Attribute.

In R XML-Modul einen Satz von Werten von xpathSApply(), seine XPath 1.0 Funktion zu extrahieren, man das fun Argument als xmlValue für Elementwert und xmlAttrs für Attributwerte angeben würde. Von dort aus können Sie die ausgegebene Liste oder Matrix für die Datenrahmenmigration bearbeiten. Und speziell für Ihre Bedürfnisse können Sie einfach Daten in eine Matrix extrahieren und in den endgültigen Datenrahmen transponieren. Die double forward slash wird in XPath-Ausdruck verwendet, um irgendwo im Dokument einen bestimmten Ort, hier Kunden, zu finden.

library(XML) 
xmlstr <- '<config> 
      <competition id="0" name="0" pomId="1.3.1-SNAPSHOT" timeslotLength="60" bootstrapTimeslotCount="336" bootstrapDiscardedTimeslots="24" timeslotsOpen="24" deactivateTimeslotsAhead="1" minimumOrderQuantity="0.01" timezoneOffset="-6" latitude="45" simulationRate="720" simulationModulo="3600000"> 
       <description/> 
       <simulationBaseTime> 
        <iMillis>1255132800000</iMillis> 
       </simulationBaseTime> 
       <broker>default broker</broker> 
       <customer id="4097" name="HighIncome-2_8" population="1" powerType="ELECTRIC_VEHICLE" customerClass="SMALL" controllableKW="-3.3" upRegulationKW="-3.3" downRegulationKW="3.3" storageCapacity="85.0" multiContracting="false" canNegotiate="false"/> 
       <customer id="4100" name="HighIncome-2_9" population="1" powerType="ELECTRIC_VEHICLE" customerClass="SMALL" controllableKW="-3.3" upRegulationKW="-3.3" downRegulationKW="3.3" storageCapacity="60.0" multiContracting="false" canNegotiate="false"/> 
       <customer id="4103" name="HighIncome-2_10" population="1" powerType="ELECTRIC_VEHICLE" customerClass="SMALL" controllableKW="-3.3" upRegulationKW="-3.3" downRegulationKW="3.3" storageCapacity="60.0" multiContracting="false" canNegotiate="false"/> 
       <customer id="4106" name="HighIncome-2_11" population="1" powerType="ELECTRIC_VEHICLE" customerClass="SMALL" controllableKW="-3.3" upRegulationKW="-3.3" downRegulationKW="3.3" storageCapacity="85.0" multiContracting="false" canNegotiate="false"/> 
      </competition> 
      </config>'  
xml <- xmlParse(xmlstr) 

# MATRIX OF CUSTOMER ATTRIBS 
customerAttribs <- xpathSApply(doc=xml, path="//customer", xmlAttrs) 

# TRANSPOSE TO DATA FRAME 
df <- data.frame(t(customerAttribs)) 

#  id   name population  powerType customerClass controllableKW \ 
# 1 4097 HighIncome-2_8   1 ELECTRIC_VEHICLE   SMALL   -3.3 
# 2 4100 HighIncome-2_9   1 ELECTRIC_VEHICLE   SMALL   -3.3 
# 3 4103 HighIncome-2_10   1 ELECTRIC_VEHICLE   SMALL   -3.3 
# 4 4106 HighIncome-2_11   1 ELECTRIC_VEHICLE   SMALL   -3.3 
# upRegulationKW downRegulationKW storageCapacity multiContracting canNegotiate 
# 1   -3.3    3.3   85.0   false  false 
# 2   -3.3    3.3   60.0   false  false 
# 3   -3.3    3.3   60.0   false  false 
# 4   -3.3    3.3   85.0   false  false