jastrachan | 22 Jul 10:01 2004
Picon

Re: GPath alternative


On 22 Jul 2004, at 08:57, John Wilson wrote:

>
> On 22 Jul 2004, at 06:08, 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.
>>
>  Hi Christopher!
>
> I presume you don't like the [0] bits?
>
>
> Using text() rather than value() gets rid of some of this
>
> 		rss = new XmlParser().parse(xml)
>
> 		println "${rss[' <at> version']}"
>
> 		for (item in rss.channel.item) {
>
> 			println "${item.title[0].text()} at ${item.link[0].text()}"
>
> 		}
>
> or more GPath like:
> 				
> 		rss.channel.item.each{println "${it.title[0].text()} at 
> ${it.link[0].text()}"}
>
> Using org.codehaus.groovy.sandbox.util.XmlSlurper get rid of them all
>
> 		rss = new XmlSlurper().parse(xml)
>
> 		println "${rss[' <at> version']}"
>
> 		for (item in rss.channel.item) {
>
> 			println "${item.title.text()} at ${item.link.text()}"
>
> 		}
> 				
> 		rss.channel.item.each{println "${it.title.text()} at 
> ${it.link.text()}"}

As I just mentioned on Yuri's mail - you could overload toString() (or 
implement Writable) in your XmlSlurper to avoid the .text() bit at the 
end.

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


Gmane