# REXML parsing issue

**URL:** <https://www.ruby-forum.com/t/rexml-parsing-issue/119790>\
**Category:** Ruby\
**Created:** [November 10, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/rexml-parsing-issue/119790 "2007-11-10T15:44:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [November 10, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/rexml-parsing-issue/119790/1 "2007-11-10T15:44:14Z")

</div>

Disclaimer: I’m new to programming and ruby.

I’ve used REXML to write both tree and stream parsers for simple xml  
files but I can’t seem to return values for the xml below in either  
case:

 Sample Text

In this example I’d like to return the value (Sample Text)for the tag? So my question is how do I specify this attribute to  
either a tree or stream parser?

Any help would be appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [November 10, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/rexml-parsing-issue/119790/2 "2007-11-10T16:15:38Z")

</div>

Charles Sword wrote:

> either a tree or stream parser?  
> Use XPath:

title = REXML::XPath.first(doc, ‘/HIT/FIELD[“title” = @name]’).text

Look up my assert\_xpath project for more XPath scenarios.

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [November 10, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/rexml-parsing-issue/119790/3 "2007-11-10T20:20:48Z")

</div>

Thanks Phlip. That allowed me to get to the first instance in each  
document but I’m struggling with populating an array with XPath.each?  
Here’s the statement I’m using:

url=REXML::XPath.each(doc, ‘//HIT/FIELD[“url” = @NAME]’).text  
{|element|}

I’m getting a ‘no code block given’ error but I thought this should pass  
each element back to the array url?

Thanks in advance for any further help.

charles

Phlip wrote:

> Charles Sword wrote:
> 
> > either a tree or stream parser?  
> > Use XPath:
> 
> title = REXML::XPath.first(doc, ‘/HIT/FIELD[“title” = @name]’).text
> 
> Look up my assert\_xpath project for more XPath scenarios.

---

<div class="post-metadata">

**Author:** ![valdar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5a626/32.png) [@valdar](https://www.ruby-forum.com/u/valdar)\
**Post date:** [November 10, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/rexml-parsing-issue/119790/4 "2007-11-10T20:37:28Z")

</div>

Disregard previous post. I was able to work through how to iterate  
through the rest of the document.

Thanks again for your help.

Charles Sword wrote:

> Thanks Phlip. That allowed me to get to the first instance in each  
> document but I’m struggling with populating an array with XPath.each?  
> Here’s the statement I’m using:
> 
> url=REXML::XPath.each(doc, ‘//HIT/FIELD[“url” = @NAME]’).text  
> {|element|}
> 
> I’m getting a ‘no code block given’ error but I thought this should pass  
> each element back to the array url?
> 
> Thanks in advance for any further help.
> 
> charles
> 
> Phlip wrote:
> 
> > Charles Sword wrote:
> > 
> > > either a tree or stream parser?  
> > > Use XPath:
> > 
> > title = REXML::XPath.first(doc, ‘/HIT/FIELD[“title” = @name]’).text
> > 
> > Look up my assert\_xpath project for more XPath scenarios.
