Skip to content

Parsing XML to tree.

4 messages · avinash sahu, Ben Tupper, Gabor Grothendieck

#
Hi,
On May 8, 2013, at 3:43 AM, avinash sahu wrote:

            
Why don't you show us the steps that you have tried?  I hope you are using the XML package for this; if not, I highly recommend it.

http://cran.r-project.org/web/packages/XML/index.html

Cheers,
Ben
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org
#
On Wed, May 8, 2013 at 3:43 AM, avinash sahu <avinash.sahu at gmail.com> wrote:
Try this:

library(XML)

URL <- "http://www.emouseatlas.org/emap/ema/theiler_stages/StageDefinition/Stage_xml/TS23.xml"
root <- xmlTreeParse(URL, useInternalNodes = TRUE)

fn <- function(node) {
   id <- xmlAttrs(node)["id"]
   parent.id <- xmlAttrs(xmlParent(node))["id"]
   setNames(head(c(id, parent.id, NA), 2), c("id", "parent"))
}
parents <- t(xpathSApply(root, "//component", fn))

parents[1:4, ]

The last line shows the first 4 child/parent pairs as follows:

     id     parent
[1,] "7148" NA
[2,] "7149" "7148"
[3,] "7150" "7149"
[4,] "7151" "7150"



--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com