The Many Ways to Philly (Latitude)
Using Data Structures
Using the indexing list notation from R we can get to the nodes we need
> lat<-xmlResult[['doc']][['ResultSet']][['Result']][['Latitude']][['text']]
> long<-xmlResult[['doc']][['ResultSet']][['Result']][['Longitude']][['text']]
> lat
39.951405
looks good, but if we examine this further
> str(lat)
list()
- attr(*, "class")= chr [1:5] "XMLTextNode" "XMLNode" "RXMLAbstractNode" "XML...
Although it has a decent display value this
variable still considers itself an XMLNode and contains no index to
obtain raw leaf value we want—the descriptor just says list()
instead of something we can use (like
$lat
). We’re not quite there
yet.
Using Helper Methods
Fortunately, the XML package offers a method to access the leaf
value: xmlValue
> lat<-xmlValue(xmlResult[['doc']][['ResultSet']][['Result']][['Latitude']])
> str(lat)
chr "39.951405"
Using Internal Class Methods
There are usually multiple ways to accomplish the same task in R. Another means to get to this our character lat/long data is to use the “value” method provided by the node itself
> lat<-xmlResult[['doc']][['ResultSet']][['Result']][['Latitude']][['text']]$value
If we were really clever we would have understood that XML doc class provided us with useful methods all the way down! Try neurotically holding down the tab key after typing
> lat<-xmlResult$
(now hold down the tab key)
xmlResult$doc xmlResult$dtd
(let's go with doc and start looking for more methods using $)
> lat<-xmlResult$doc$ ...
Get Data Mashups in R now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.