jastrachan | 22 Jul 10:00 2004
Picon

Re: GPath alternative

Good idea - though its tricky.

how do we know if

	item.title

is meant to return just first one or a collection of title elements?

FWIW if there's only one, we could return one and then overload  
toString() to output the text of the element, which would do what you  
wanted

On 22 Jul 2004, at 08:55, Yuri Schimke wrote:
> I haven't used this Groovy XML stuff before.  But I think its a case  
> of correctness vs conciseness.
>
> I think a subclassed LeniantXmlParser would be useful.  Forsaking  
> correctness, for simplicity.  XML gurus will hate this :)
>
> rss.version as a shortcut to rss[' <at> version']
>
> item.title as a shortcut to item.title.value()[0].  Gets the first  
> attribute or child node, and returns value if its a text node.  Even  
> when multiple nodes are possible, this could still work if you iterate  
> through results using each().  Since each on a single object, still  
> works.
> 	
> So it would be.
>
> println "${rss.version}" 
>
> for (item in rss.channel.item) {
>   println "${item.title} at ${item.link}"
> }
>
> On 22/07/2004, at 8:08 AM, Christopher M. Judd wrote:
>
>> Ok, I figured out how to convert a InputStream to a XML Node I can  
>> use GPath on. But I must be missing something; I thought it would be  
>> easier to print the value of an element. In particular, I was trying  
>> to print out the item title and item link.
>>
>>  
>>
>> Code:
>>
>>  
>>
>> content = url.openStream()
>>
>>  
>>
>> rss = new XmlParser().parse(content)
>>
>>  
>>
>> println "${rss[' <at> version']}"
>>
>>  
>>
>> for (item in rss.channel.item) {
>>
>>   println "${item.title[0].value()[0]} at ${item.link[0].value()[0]}"
>>
>> }
>>
>>  
>>
>> XML:
>>
>>  
>>
>> <rss version="0.92">
>>
>>  <channel>
>>
>>   <title>Space: Groovy (GROOVY)</title>
>>
>>    <description />
>>
>>    <link>http://docs.codehaus.org/display/GROOVY</link>
>>
>>    <item>
>>
>>     <title>Page Updated : GROOVY:Differences from Java</title>
>>
>>     
>>   <link>http://docs.codehaus.org/display/GROOVY/ 
>> Differences+from+Java</link>
>>
>>    </item>
>>
>>   <item>
>>
>>     <title>Page Updated : GROOVY:Unit Testing</title>
>>
>>      <link>http://docs.codehaus.org/display/GROOVY/Unit+Testing</link>
>>
>>    </item>
>>
>>  </channel>
>>
>> </rss>
>>
>>  
>>
>>  
>>
>> -----------------------------------------------
>>
>> Christopher M. Judd
>>
>> Judd Solutions, LLC
>>
>> President & Consultant
>>
>> Co-author of Enterprise Java Development on a Budget
>>
>>  
>>
>> 685 Farrington Dr.
>>
>> Worthington,  OH 43085
>>
>> phone: 614-378-4119
>>
>> email: cjudd@...
>>
>> web: www.juddsolutions.com
>>
>>  
>>

James
-------
http://radio.weblogs.com/0112098/

Gmane