[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

REXML parsing issue

Charles Sword

11/10/2007 2:44:00 PM

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:

<HIT NO="1" RANK="15335" SITEID="0" MOREHITS="0" FCOCOUNT="0">
<FIELD NAME="title">Sample Text</FIELD>
</HIT>

In this example I'd like to return the value (Sample Text)for the <FIELD
NAME="title"> tag? So my question is how do I specify this attribute to
either a tree or stream parser?

Any help would be appreciated.

Thanks.
--
Posted via http://www.ruby-....

3 Answers

Phlip

11/10/2007 3:12:00 PM

0

Charles Sword wrote:

> 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:
>
> <HIT NO="1" RANK="15335" SITEID="0" MOREHITS="0" FCOCOUNT="0">
> <FIELD NAME="title">Sample Text</FIELD>
> </HIT>
>
> In this example I'd like to return the value (Sample Text)for the <FIELD
> NAME="title"> tag? So my question is how do I specify this attribute to
> 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.

--
Phlip

Charles Sword

11/10/2007 7:21:00 PM

0

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.

--
Posted via http://www.ruby-....

Charles Sword

11/10/2007 7:38:00 PM

0

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.

--
Posted via http://www.ruby-....