0

I am trying to convert my xml data to a data frame in order to export it as a csv. The XML data is below. When I use the code finalresults <- xmlToDataFrame(results), all of the data is put into one cell under the element column. Does anyone know how I can parse this data out? What I really need is the 20495 number under duration_in_traffic. Thank you!

> xmlChildren(results$row[[1L]])
$status
<status>OK</status> 

$duration
<duration>
  <value>20231</value>
  <text>5 hours 37 mins</text>
</duration> 

$distance
<distance>
  <value>459266</value>
  <text>459 km</text>
</distance> 

$duration_in_traffic
<duration_in_traffic>
  <value>20495</value>
  <text>5 hours 42 mins</text>
</duration_in_traffic> 

attr(,"class")
[1] "XMLInternalNodeList" "XMLNodeList" 
  • Are you getting the data from Google? – SymbolixAU Jan 25 '17 at 00:36
  • We'd kinda need the original XML data to assist. – hrbrmstr Jan 25 '17 at 00:48
  • 1
    [this looks like](http://stackoverflow.com/q/16863018/5977215) a very similar question – SymbolixAU Jan 25 '17 at 00:49
  • The original data is generated from the source code of the function gmapsdistance in this post: http://stackoverflow.com/questions/41133859/r-code-gmapsdistance – Gabby Potvin Jan 25 '17 at 11:06
  • It's not generally a good idea to link to other posts for data and code that you want to use as part of your question. Each question should be self-contained, reproducible, and show the issue you're having. – SymbolixAU Jan 26 '17 at 22:07

1 Answers1

0

If you just want the duration_in_traffic node (and if that only has one level), then use that node set as input to xmlToDataFrame

xmlToDataFrame(doc["//duration_in_traffic"])

You might be able to get all row values using this

rows <-  lapply( doc['//row'], function(x) data.frame(xmlToList(x) ))    
do.call("rbind", rows)
Chris S.
  • 2,185
  • 1
  • 14
  • 14