On Wed, May 8, 2013 at 3:43 AM, avinash sahu <avinash.s...@gmail.com> wrote:
> Hi All,
>
> I am struggling to parse a XML file that describes a tree. The XML file is
> present here:
> http://www.emouseatlas.org/emap/ema/theiler_stages/StageDefinition/Stage_xml/TS23.xml
> I want is simple list of parents id of each component. The output will look
> like
> Component = [7148  7149   7150   7151..... 7177..............]
> Parents=        [NA       7148   7149  7150....  7148..............]
> I meant if components are arranged from 7148 to 8419, I need their parents
> id.
>

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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to