# Recursing XML data using REXML

**URL:** <https://www.ruby-forum.com/t/recursing-xml-data-using-rexml/122772>\
**Category:** Ruby\
**Created:** [December 12, 2007, 12:59am UTC](https://www.ruby-forum.com/t/recursing-xml-data-using-rexml/122772 "2007-12-12T00:59:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbourne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a88e57/32.png) [@cbourne](https://www.ruby-forum.com/u/cbourne)\
**Post date:** [December 12, 2007, 12:59am UTC](https://www.ruby-forum.com/t/recursing-xml-data-using-rexml/122772/1 "2007-12-12T00:59:30Z")

</div>

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:

 Test Rule This is a test rule 2 

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

# Rule Name - Test Rule

# Rule Description - Test Rule

- group operator is all - condition name is condition 1 
  - group operator is not - condition name is condition 2 
    - group operator is not - condition name is condition 3

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

---

<div class="post-metadata">

**Author:** ![cbourne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a88e57/32.png) [@cbourne](https://www.ruby-forum.com/u/cbourne)\
**Post date:** [December 12, 2007, 3:51pm UTC](https://www.ruby-forum.com/t/recursing-xml-data-using-rexml/122772/2 "2007-12-12T15:51:47Z")

</div>

Answer here:  
[http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/5a948a058d3fac18](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/5a948a058d3fac18)

---

<div class="post-metadata">

**Author:** ![cbourne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a88e57/32.png) [@cbourne](https://www.ruby-forum.com/u/cbourne)\
**Post date:** [December 12, 2007, 5:18pm UTC](https://www.ruby-forum.com/t/recursing-xml-data-using-rexml/122772/3 "2007-12-12T17:18:21Z")

</div>

Gavin K. wrote:

> Answer here:  
> [http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/5a948a058d3fac18](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

---

<div class="post-metadata">

**Author:** ![cbourne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a88e57/32.png) [@cbourne](https://www.ruby-forum.com/u/cbourne)\
**Post date:** [December 12, 2007, 5:36pm UTC](https://www.ruby-forum.com/t/recursing-xml-data-using-rexml/122772/4 "2007-12-12T17:36:23Z")

</div>

Carl B. wrote:

> Gavin K. wrote:
> 
> > Answer here:  
> > [http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/5a948a058d3fac18](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!

Carl

---

<div class="post-metadata">

**Author:** ![cbourne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a88e57/32.png) [@cbourne](https://www.ruby-forum.com/u/cbourne)\
**Post date:** [December 12, 2007, 6:54pm UTC](https://www.ruby-forum.com/t/recursing-xml-data-using-rexml/122772/5 "2007-12-12T18:54:04Z")

</div>

On Dec 11, 4:59 pm, Carl B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![cbourne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a88e57/32.png) [@cbourne](https://www.ruby-forum.com/u/cbourne)\
**Post date:** [December 12, 2007, 5:30pm UTC](https://www.ruby-forum.com/t/recursing-xml-data-using-rexml/122772/6 "2007-12-12T17:30:15Z")

</div>

Carl B. wrote:

> Gavin K. wrote:
> 
> > Answer here:  
> > [http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/5a948a058d3fac18](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](http://groups.google.com/group/comp.lang.ruby)  
and find the message titled “Recursing XML data using REXML”.
