[R] Need help extracting info from XML file using XML package

Romain Francois romain.francois at dbmail.com
Mon Mar 2 13:29:30 CET 2009


Romain Francois wrote:
> Hi,
>
> You also might want to check R4X:
>
> # install.packages("R4X", repos="http://R-Forge.R-project.org")
> require( "R4X" )
> x <- xml("http://code.google.com/apis/kml/documentation/KML_Samples.kml")
> coords <- x["////Polygon///coordinates/#" ]
> data <- sapply( strsplit( coords, "(,|\\s+)" ), as.numeric )
>
> Romain
>
With a bit more formatting :

# install.packages("R4X", repos="http://R-Forge.R-project.org")
require( "R4X" )
x <- xml("http://code.google.com/apis/kml/documentation/KML_Samples.kml")
coords <- x["////Polygon///coordinates/#" ]
data <- lapply( strsplit( coords, "(,|\\s+)" ), function(.){
  out <- matrix( as.numeric(.), ncol = 3, byrow = TRUE )
  colnames( out ) <- c("longitude", "lattitude", "altitude" )
  out
})
names( data ) <- x["//Placemark/name/#" ]

Romain

-- 
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr




More information about the R-help mailing list