[R] XML to data frame or list

bogdan romocea br44114 at yahoo.com
Sun Mar 13 14:58:40 CET 2005


I managed to parse more complex XML files as well. The trick was to
manually determine the position of the child nodes of interest, after
which they can be parsed in a loop. For example:

require(XML)
doc <- xmlTreeParse("file.xml",getDTD=T,addAttributeNamespaces=T)
r <- xmlRoot(doc)

#find the nodes of interest
r[[i]][[j]]....

#then read them
xmldata <- list(NULL)
for (i in 1:xmlSize(r[[2]][[1]])) {
  xmldata[[i]] <- as.data.frame(xmlSApply(r[[2]][[1]][[i]],xmlValue))
  }


--- Barry Rowlingson <B.Rowlingson at lancaster.ac.uk> wrote:
> Gabor Grothendieck wrote:
> 
> >
> > You could check out the ctv package that was recently announced.
> > It uses XML so its source would provide an example.
> >
> > If its a one-time operation, Excel reads XML and you could then
> > use one of the many Excel to R possibilities.
> 
>   For an xml file like this:
> 
> <?xml version="1.0"?>
> <variables>
> <a>100</a>
> <b>23</b>
> <z>666</z>
> </variables>
> 
> its a one-liner with the XML package (library(XML)):
> 
> xmlReadSimple <-
> function(xmlFile){
>    as.list(xmlSApply(xmlRoot(xmlTreeParse(xmlFile)),xmlValue))
> }
> 
> add an lapply(...,as.numeric) for conversion to numbers.
> 
>   sweet.
> 
> Baz
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>   
> 
> __________________________________________________



More information about the R-help mailing list