# Help needed with rexml

**URL:** <https://rubytalk.org/t/help-needed-with-rexml/20453>\
**Category:** ruby-talk\
**Created:** [28 August 2005 18:16 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453 "2005-08-28T18:16:32Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Forrest](https://avatars.discourse-cdn.com/v4/letter/f/77aa72/32.png) [@Forrest](https://rubytalk.org/u/Forrest)\
**Post date:** [28 August 2005 18:16 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/1 "2005-08-28T18:16:32Z")

</div>

I've been struggling to properly parse some XML with rexml. I will fully  
admit my XML ignorance in advance. It would be easy enough to parse  
this with a regular expression instead, but I would prefer to use the  
right tool.

Here's a sample XML response:

\<?xml version='1.0' encoding="iso-8859-1" ?\>  
\<methodResponse\>  
&nbsp;&nbsp;\<fault\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<value\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<struct\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<member\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<name\>faultCode\</name\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<value\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<int\>5\</int\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</value\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</member\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<member\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<name\>faultString\</name\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<value\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<string\>system error (nologin)\</string\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</value\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</member\>  
&nbsp;&nbsp;&nbsp;&nbsp;\</struct\>  
&nbsp;&nbsp;\</value\>  
&nbsp;&nbsp;\</fault\>  
\</methodResponse\>

However, I can't anything useful out of it. For instance, I've been  
trying something like this:

require 'rexml/document'

file = File.new("test.xml")  
xml = REXML::Document.new(file)  
xml.elements.each { |i|  
&nbsp;&nbsp;i.texts.each { |t|  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Class: #{t.class}"  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Value: #{t.value}"  
&nbsp;&nbsp;&nbsp;&nbsp;puts "String: #{t.to\_s}"  
&nbsp;&nbsp;}  
}

This doesn't print anything useful for the class. Where am I going wrong  
with this? I've been digging through the documentation but I'm must not  
getting it.

For what it's worth, I can parse this in perl easily enough (which  
suggests to me the XML is valid):

use Data::Dumper;  
use XML::Simple; # AKA "XML For Idiots"

my $ref = XMLin("test.xml"); # A file containing the XML above  
print Dumper $ref, "\n";

I can then use the results to figure out how to dereference $ref to pull  
the error information returned by the server.

Responses to the list or the newsgroup, please, for future googlers  
to find.

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [28 August 2005 19:27 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/2 "2005-08-28T19:27:48Z")

</div>

> I've been struggling to properly parse some XML with rexml. I will fully  
> admit my XML ignorance in advance. It would be easy enough to parse  
> this with a regular expression instead, but I would prefer to use the  
> right tool.
> 
> Here's a sample XML response:
> 
> \<?xml version='1.0' encoding="iso-8859-1" ?\>  
> \<methodResponse\>  
> &nbsp;&nbsp;\<fault\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<value\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<struct\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<member\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<name\>faultCode\</name\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</value\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</member\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</struct\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</value\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</fault\>  
> \</methodResponse\>
> 
> However, I can't anything useful out of it. For instance, I've been  
> trying something like this:
> 
> require 'rexml/document'
> 
> file = File.new("test.xml")  
> xml = REXML::Document.new(file)  
> xml.elements.each { |i|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i.texts.each { |t|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Class: #{t.class}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Value: #{t.value}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "String: #{t.to\_s}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> }

I'd start with something like this (untested, from memory):

xml.elements.each do |elem|  
&nbsp;&nbsp;p elem.node\_type  
&nbsp;&nbsp;p elem.text  
end

> This doesn't print anything useful for the class. Where am I going wrong  
> with this? I've been digging through the documentation but I'm must not  
> getting it.

What exactly do you want to extract? You'll likely want some kind of  
XPath expression with #each like in the tutorial.

Did you look at the tutorial?  
[http://www.germane-software.com/software/rexml/docs/tutorial.html](http://www.germane-software.com/software/rexml/docs/tutorial.html)

Kind regards

robert

> **···**
>
> 2005/8/28, Michael \<invalid@dev.null\>:

---

<div class="post-metadata">

**Author:** ![Francois\_Montel](https://avatars.discourse-cdn.com/v4/letter/f/65b543/32.png) [@Francois\_Montel](https://rubytalk.org/u/Francois_Montel)\
**Post date:** [28 August 2005 19:36 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/3 "2005-08-28T19:36:22Z")

</div>

Michael, I came across the same problem recently when using ruby/rexml  
for the first time.

The reason why you're not getting results is because each\_element and  
each\_element\_with\_attribute commands only iterate through the element's  
immediate children. They don't recurse through all the descendants. So  
what you're probably getting is just the root element and none of the  
children.

If you need to iterate through all the elements in the whole document,  
then use the XPath.each command. For example XPath.each('/////methods')  
{ |x| whatever you want to do with them } should work. That's what I  
finally had to do in my recent experience. I'm not sure what XPath  
search you would use to go through ALL of the elements in the document,  
but with some experimentaiton you'll probably find it. (And post what  
you find!)

There may be a better way to do this and I posted something about this  
a couple of days ago, but received no response. It seems that  
each\_element and each\_element\_with\_attribute should include an option  
to recurse through all the descendents, but unfortuantely it doesn't  
seem to (or at least I couldn't find it).

Maybe that could be added to a new release of rexml (if the dev is  
reading this?)

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [29 August 2005 05:15 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/4 "2005-08-29T05:15:01Z")

</div>

What type of information do you want to get out of this? You never posted what you \*thought\* your sample ruby code would give you. I ran your perl example, and it looks like the xml document but where \< \> are gsub'd for { }.

Here is an example which shows some xpath usage.:

require 'rexml/document'

file = File.new("test.xml")  
root = REXML::Document.new(file).root  
fault\_arr = root.elements.each( "fault" ) do |e1|  
&nbsp;&nbsp;&nbsp;e1.elements.each( "value/struct/member" ) do |e2|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e2.elements.each( '\*' ) { |e3| print e3.text.strip }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e2.elements.each( '\*/\*' ){ |e3| puts " " + e3.text.strip }  
&nbsp;&nbsp;&nbsp;end  
end

puts "Message response faulted!" if fault\_arr.length \> 0

Zach

Michael wrote:

> **···**
>
> > I've been struggling to properly parse some XML with rexml. I will fully  
> > admit my XML ignorance in advance. It would be easy enough to parse  
> > this with a regular expression instead, but I would prefer to use the  
> > right tool.
> > 
> > Here's a sample XML response:
> > 
> > \<?xml version='1.0' encoding="iso-8859-1" ?\>  
> > \<methodResponse\>  
> > &nbsp;&nbsp;\<fault\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\<value\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<struct\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<member\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<name\>faultCode\</name\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<value\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<int\>5\</int\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</value\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</member\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<member\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<name\>faultString\</name\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<value\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<string\>system error (nologin)\</string\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</value\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</member\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\</struct\>  
> > &nbsp;&nbsp;\</value\>  
> > &nbsp;&nbsp;\</fault\>  
> > \</methodResponse\>
> > 
> > However, I can't anything useful out of it. For instance, I've been  
> > trying something like this:
> > 
> > require 'rexml/document'
> > 
> > file = File.new("test.xml")  
> > xml = REXML::Document.new(file)  
> > xml.elements.each { |i|  
> > &nbsp;&nbsp;i.texts.each { |t|  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "Class: #{t.class}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "Value: #{t.value}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "String: #{t.to\_s}"  
> > &nbsp;&nbsp;}  
> > }
> > 
> > This doesn't print anything useful for the class. Where am I going wrong  
> > with this? I've been digging through the documentation but I'm must not  
> > getting it.
> > 
> > For what it's worth, I can parse this in perl easily enough (which  
> > suggests to me the XML is valid):
> > 
> > use Data::Dumper;  
> > use XML::Simple; # AKA "XML For Idiots"
> > 
> > my $ref = XMLin("test.xml"); # A file containing the XML above  
> > print Dumper $ref, "\n";
> > 
> > I can then use the results to figure out how to dereference $ref to pull  
> > the error information returned by the server.
> > 
> > Responses to the list or the newsgroup, please, for future googlers  
> > to find.

---

<div class="post-metadata">

**Author:** ![Francois\_Montel](https://avatars.discourse-cdn.com/v4/letter/f/65b543/32.png) [@Francois\_Montel](https://rubytalk.org/u/Francois_Montel)\
**Post date:** [28 August 2005 21:46 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/5 "2005-08-28T21:46:23Z")

</div>

PS. There's a great XML plugin for JEdit that will show you the results  
of an XPath search on your document. That way you can try out different  
variations until you get the result set that you want without having to  
run your script each time to test it.

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [28 August 2005 21:51 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/6 "2005-08-28T21:51:22Z")

</div>

Written off teh top of my head, but you could write your own.

&nbsp;&nbsp;class REXML::Element  
&nbsp;&nbsp;&nbsp;&nbsp;def each\_element\_recurse  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each\_element { |e|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless e.children.empty? rescue false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.each\_element\_recurse  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield(e)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

I made a first stab at it b/c I will probably need it myself soon.

(Yes, I know I'm reopening a standard class! :-p)

T.

---

<div class="post-metadata">

**Author:** ![Forrest](https://avatars.discourse-cdn.com/v4/letter/f/77aa72/32.png) [@Forrest](https://rubytalk.org/u/Forrest)\
**Post date:** [29 August 2005 04:41 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/7 "2005-08-29T04:41:23Z")

</div>

In article \<1125257760.436346.52640@f14g2000cwb.googlegroups.com\>,  
&nbsp;&nbsp;"zerohalo" \<zerohalo@gmail.com\> writes:

> Michael, I came across the same problem recently when using ruby/rexml  
> for the first time.
> 
> The reason why you're not getting results is because each\_element and  
> each\_element\_with\_attribute commands only iterate through the element's  
> immediate children. They don't recurse through all the descendants. So  
> what you're probably getting is just the root element and none of the  
> children.
> 
> If you need to iterate through all the elements in the whole document,  
> then use the XPath.each command. For example XPath.each('/////methods')  
> { |x| whatever you want to do with them } should work. That's what I  
> finally had to do in my recent experience. I'm not sure what XPath  
> search you would use to go through ALL of the elements in the document,  
> but with some experimentaiton you'll probably find it. (And post what  
> you find!)
> 
> There may be a better way to do this and I posted something about this  
> a couple of days ago, but received no response. It seems that  
> each\_element and each\_element\_with\_attribute should include an option  
> to recurse through all the descendents, but unfortuantely it doesn't  
> seem to (or at least I couldn't find it).

Thanks! This is what I'm looking for. I read through the tutorial and  
the rdoc documentation but I just couldn't figure out what I was missing.

When I mentioned iterating through the document, what I'm really doing  
is describing the process I've been using for deciding how to handle  
some arbitrary XML document I wound up with. It's not ideal, I admit. It  
might do me some good to read up on XML.  
--Michael

---

<div class="post-metadata">

**Author:** ![Grice](https://avatars.discourse-cdn.com/v4/letter/g/85f322/32.png) [@Grice](https://rubytalk.org/u/Grice)\
**Post date:** [29 August 2005 04:46 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/8 "2005-08-29T04:46:26Z")

</div>

In article \<9e3fd2c8050828122755267c9@mail.gmail.com\>,  
&nbsp;&nbsp;Robert Klemme \<shortcutter@googlemail.com\> writes:

[...]

> What exactly do you want to extract? You'll likely want some kind of  
> XPath expression with #each like in the tutorial.  
> Did you look at the tutorial?  
> [http://www.germane-software.com/software/rexml/docs/tutorial.html](http://www.germane-software.com/software/rexml/docs/tutorial.html)

Thanks for the reply. I did read the tutorial--I just wasn't making the  
necessary connections.  
--Michael

---

<div class="post-metadata">

**Author:** ![Francois\_Montel](https://avatars.discourse-cdn.com/v4/letter/f/65b543/32.png) [@Francois\_Montel](https://rubytalk.org/u/Francois_Montel)\
**Post date:** [28 August 2005 21:51 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/9 "2005-08-28T21:51:22Z")

</div>

Correction to my last post. It's in the XSLT plugin.

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [28 August 2005 23:02 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/10 "2005-08-28T23:02:26Z")

</div>

Trans wrote:

> Written off teh top of my head, but you could write your own.
> 
> &nbsp;&nbsp;class REXML::Element  
> &nbsp;&nbsp;&nbsp;&nbsp;def each\_element\_recurse  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each\_element { |e|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless e.children.empty? rescue false  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.each\_element\_recurse  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield(e)  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> I made a first stab at it b/c I will probably need it myself soon.
> 
> (Yes, I know I'm reopening a standard class! :-p)

If you really think you need to visit every element you may be better off using the stream or pull parsers.

James

> **···**
>
> --
> 
> [http://www.ruby-doc.org](http://www.ruby-doc.org) - The Ruby Documentation Site  
> [http://www.rubyxml.com](http://www.rubyxml.com) - News, Articles, and Listings for Ruby & XML  
> [http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
> [http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys

---

<div class="post-metadata">

**Author:** ![Francois\_Montel](https://avatars.discourse-cdn.com/v4/letter/f/65b543/32.png) [@Francois\_Montel](https://rubytalk.org/u/Francois_Montel)\
**Post date:** [28 August 2005 23:16 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/11 "2005-08-28T23:16:25Z")

</div>

James, would you mind pointing to a link that explains how to do this?  
I couldn't find reference to it in the rexml documentation. Tx!

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [29 August 2005 04:44 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/12 "2005-08-29T04:44:40Z")

</div>

zerohalo wrote:

> James, would you mind pointing to a link that explains how to do this?  
> I couldn't find reference to it in the rexml documentation. Tx!

I don't think there is much written about the pull parser (though I should see an article of mine on the topic published in a mainstream geek mag in the next few months. I hope.)

Back in 2001 I wrote an article on using the REXML stream parser that may be relevant, and possibly accurate:

[http://www.rubyxml.com/articles/REXML/Stream\_Parsing\_with\_REXML](http://www.rubyxml.com/articles/REXML/Stream_Parsing_with_REXML)

The pull parser sits below all the other REXML parsers, and has a sparser API, but is quite handy for many things.

The basic idea is to pull events from the parser, see what you've got (start\_element? end\_element? text?), and act on it. You can also push things back onto the parse stream, too, as well as peek down the stream to see what's ahead (while not disrupting the current stream order).

# Simple example:  
require 'rexml/parsers/pullparser'

# foo.xml has  
# \<foo\>  
# \<baz\>This is baz\</baz\>  
# \<bar\>Ignore me!\</bar\>  
# \<baz\>This is baz, also\</baz\>  
# \</foo\>

File.open( 'foo.xml', 'r' ) do |f|  
&nbsp;&nbsp;&nbsp;parser = REXML::Parsers::PullParser.new( f )  
&nbsp;&nbsp;&nbsp;while parser.has\_next?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pull\_event = parser.pull  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts( "Element: " + pull\_event[0] ) if pull\_event.start\_element?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if pull\_event.start\_element? and pull\_event[0] == 'baz'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while !(pull\_event = parser.pull).end\_element?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts pull\_event[0] if pull\_event.text?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

Or something like that.

See also

[http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/Parsers/PullParser.html](http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/classes/REXML/Parsers/PullParser.html)

James

> **···**
>
> --
> 
> [http://www.ruby-doc.org](http://www.ruby-doc.org) - The Ruby Documentation Site  
> [http://www.rubyxml.com](http://www.rubyxml.com) - News, Articles, and Listings for Ruby & XML  
> [http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
> [http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys

---

<div class="post-metadata">

**Author:** ![Francois\_Montel](https://avatars.discourse-cdn.com/v4/letter/f/65b543/32.png) [@Francois\_Montel](https://rubytalk.org/u/Francois_Montel)\
**Post date:** [29 August 2005 17:11 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/13 "2005-08-29T17:11:23Z")

</div>

Thanks, James, I'll study that.

By the way,I've tried before to access [rubyxml.com](http://rubyxml.com) (which seems to be  
your site?) which I had found when googling for rexml, and there  
doesn't seem to be any way toget to past articles. Maybe there's a  
sidebar or something but it doesn't show up in Firefox or Opera on  
Linux (I can't try IE as I don't have it). Or am I missing something?

---

<div class="post-metadata">

**Author:** ![David\_Jacobs1](https://avatars.discourse-cdn.com/v4/letter/d/977dab/32.png) [@David\_Jacobs1](https://rubytalk.org/u/David_Jacobs1)\
**Post date:** [30 August 2005 07:59 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/14 "2005-08-30T07:59:38Z")

</div>

Google on REXML and you get some good results!  
Start here:  
[http://raa.ruby-lang.org/project/rexml](http://raa.ruby-lang.org/project/rexml)  
and you'll get to  
[http://www.germane-software.com/software/rexml/](http://www.germane-software.com/software/rexml/)  
and  
[http://www.germane-software.com/software/rexml/docs/tutorial.html](http://www.germane-software.com/software/rexml/docs/tutorial.html)

Have fun!  
Cheers,  
David

> **···**
>
> 2005/8/29, zerohalo \<zerohalo@gmail.com\>:
> 
> > Thanks, James, I'll study that.
> > 
> > By the way,I've tried before to access rubyxml.com (which seems to be  
> > your site?) which I had found when googling for rexml, and there  
> > doesn't seem to be any way toget to past articles. Maybe there's a  
> > sidebar or something but it doesn't show up in Firefox or Opera on  
> > Linux (I can't try IE as I don't have it). Or am I missing something?

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [31 August 2005 18:36 UTC](https://rubytalk.org/t/help-needed-with-rexml/20453/15 "2005-08-31T18:36:18Z")

</div>

zerohalo wrote:

> Thanks, James, I'll study that.
> 
> By the way,I've tried before to access rubyxml.com (which seems to be  
> your site?) which I had found when googling for rexml, and there  
> doesn't seem to be any way toget to past articles. Maybe there's a  
> sidebar or something but it doesn't show up in Firefox or Opera on  
> Linux (I can't try IE as I don't have it). Or am I missing something?

No, the site is missing some obvious UI clues for friendlier usage.

You can see past items by tweaking the URL:

[http://rubyxml.com/index.rb/2004/12](http://rubyxml.com/index.rb/2004/12)  
&nbsp;&nbsp;&nbsp;Shows items from December of 2004

[http://rubyxml.com/index.rb/2005](http://rubyxml.com/index.rb/2005)  
&nbsp;&nbsp;&nbsp;Shows items from 2005.

[http://rubyxml.com/index.rb/Articles](http://rubyxml.com/index.rb/Articles)  
&nbsp;&nbsp;&nbsp;Shows items in the Articles category

[http://rubyxml.com/index.rb/Applications](http://rubyxml.com/index.rb/Applications)  
&nbsp;&nbsp;&nbsp;Shows items in the Applications category

More or less.

James

> **···**
>
> --
> 
> [http://www.ruby-doc.org](http://www.ruby-doc.org) - The Ruby Documentation Site  
> [http://www.rubyxml.com](http://www.rubyxml.com) - News, Articles, and Listings for Ruby & XML  
> [http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
> [http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys
