Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Recursing XML data using REXML

11 views
Skip to first unread message

Carl Bourne

unread,
Dec 11, 2007, 6:59:28 PM12/11/07
to
Hi,

I would like to process an XML file using REXML and recurse down through
some sub elements, e.g. the "group" and "cond" elements in the following
XML:

<rule>
<name>Test Rule</name>
<description>This is a test rule</description>
<order>2</order>
<group operator="all">
<cond name="condition1"/>
<group operator="not">
<cond name="condition2"/>
<group operator="not">
<cond name="condition3"/>
</group>
</group>
</group>
</rule>

However, I need to produce output that maintains the hierarchy so it can
be displayed as a nested list such as:-

<h1>Rule Name - Test Rule</h1>
<h1>Rule Description - Test Rule</h1>
<ul>
<li>group operator is all - condition name is condition 1
<ul>
<li>group operator is not - condition name is condition 2
<ul>
<li>group operator is not - condition name is condition 3</li>
</ul>
</li>
</ul>
</li>
</ul>

I can't seem to find any examples of doing this, would somebody be so
kind as to provide me with an example of how I could do this?

Regards,

Carl
--
Posted via http://www.ruby-forum.com/.

Phrogz

unread,
Dec 11, 2007, 7:05:36 PM12/11/07
to
On Dec 11, 4:59 pm, Carl Bourne <carl.bou...@intellect.co.uk> wrote:
> I would like to process an XML file using REXML and recurse down through
> some sub elements, e.g. the "group" and "cond" elements in the following
> XML:
[snip]

> However, I need to produce output that maintains the hierarchy so it can
> be displayed as a nested list such as:-

def show_element( element, level=0 )
print " "*level

# do something with the element here

# Depth-first recursion
element.children.each{ |child|
show_element( child, level+1 )
}
end

Gavin Kistner

unread,
Dec 12, 2007, 9:51:45 AM12/12/07
to

Carl Bourne

unread,
Dec 12, 2007, 11:18:20 AM12/12/07
to
Gavin Kistner wrote:
> Answer here:
> http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/5a948a058d3fac18

Thanks for this Gavin, however the link to this seems to be broken.
Please could you confirm it.

Best Regards,

Carl

Gavin Kistner

unread,
Dec 12, 2007, 11:30:16 AM12/12/07
to
Carl Bourne wrote:
> Gavin Kistner wrote:
>> Answer here:
>> http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/5a948a058d3fac18
>
> Thanks for this Gavin, however the link to this seems to be broken.
> Please could you confirm it.

It works for me. If it's not for you, you can also go to:
http://groups.google.com/group/comp.lang.ruby
and find the message titled "Recursing XML data using REXML".

Carl Bourne

unread,
Dec 12, 2007, 11:36:23 AM12/12/07
to
Carl Bourne wrote:
> Gavin Kistner wrote:
>> Answer here:
>> http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/5a948a058d3fac18
>
> Thanks for this Gavin, however the link to this seems to be broken.
> Please could you confirm it.
>
> Best Regards,
>
> Carl

Thanks Gavin,

Seems to work OK now - will give it a try. Thanks again!

0 new messages