xml to r data extraction

2019-04-12 13:43发布

问题:

I need to extract data from xml file and plot graphs of: depth vs timestamp. Heading should be the IFC code. I tried using xmlToList and xmlTodataframe, but I failed doing so. I need help. My xml file looks like

 <document>
    <site>
       <IFC_code>HONEYCR01</IFC_code>
       <Latitude>41.960161</Latitude>
       <Longitude>-90.470759</Longitude>
       <River>Honey Creek</River>
       <Road>Hwy 136, 1st Street</Road>
       <Town>Charlotte</Town>
       <from_sensor_to_river_bottom>9.35</from_sensor_to_river_bottom>
       <Unit>foot</Unit>
    </site>
    <data>
       <value>
          <timestamp>2012-05-17 15:30:03-05</timestamp>
          <depth>8.53</depth>
       </value>
       <value>
          <timestamp>2012-05-17 14:30:06-05</timestamp>
          <depth>8.50</depth>
       </value>
       <value>
          <timestamp>2012-05-17 14:15:02-05</timestamp>
          <depth>8.51</depth>
       </value>
       <value>
          <timestamp>2012-05-17 14:00:12-05</timestamp>
          <depth>8.50</depth>
       </value>
       <value>
          <timestamp>2012-05-17 13:45:08-05</timestamp>
          <depth>8.51</depth>
       </value>
      </data>
    </document>

回答1:

It seems to work:

library(XML)
doc <- xmlParse("a.xml")
xmlToDataFrame(
  getNodeSet(doc, "//value"),
  colClasses=c("character","numeric")
)