Skip to content
Back to formatted view

Raw Message

Message-ID: <49ABD12A.1020806@dbmail.com>
Date: 2009-03-02T12:29:30Z
From: Romain Francois
Subject: Need help extracting info from XML file using XML package
In-Reply-To: <49ABC8FD.1090808@dbmail.com>

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