Skip to content
Back to formatted view

Raw Message

Message-ID: <50925874.2030805@aalto.fi>
Date: 2012-11-01T11:09:40Z
From: Mikko Korpela
Subject: How to parse xml with same key name ?
In-Reply-To: <1351744198561-4648097.post@n4.nabble.com>

On 01.11.2012 06:29, Manish Gupta wrote:
> HI, 
> 
> I need to parse an xml where key name are same but values are different. 
> 
> <root>
>      <test>
>                  Some dummy text
>      </test>
>      <node id="1">one</node>
>      <node id="2">two</node>
>      <node id="3">three</node>
> </root>
> 
> When i use xmlGetAttr() function i always get one as value.  How can i get
> one , two and three? Pls help me out of this pblm .

Hi!

I would start by looking at "A Short Introduction to the XML package for
R". The xPath part of the document seems relevant to your problem.
http://www.omegahat.org/RSXML/shortIntro.html

I'm not exactly sure of what you are trying to do, but here's a start:

foo <- xmlInternalTreeParse("yourfile.xml")
nodes <- getNodeSet(foo, "//node[@id]")
sapply(nodes, xmlGetAttr, "id")
sapply(nodes, xmlValue)

-- 
Mikko Korpela
Aalto University School of Science
Department of Information and Computer Science