Database Reference
In-Depth Information
The Many Ways to Philly (Latitude)
There are three different ways we can extract the latitude and longitude coordinates from our
XML result.
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
This looks good, but examine this further:
> str(lat)
list()
- attr(*, "class")= chr [1:5] "XMLTextNode" "XMLNode"
"RXMLAbstractNode" "XML...
Although it has a decent displayvalue, this variable still considers itself an XMLNode and con-
tains no index to obtain the 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
our character lat/long data is to use the value method provided by the node itself:
>
lat<-xmlResult[['doc']][['ResultSet']][['Result']][['Latitude']][['text']]$value
Search WWH ::




Custom Search