« Return to Thread: XMLParser: attribute gets truncated when updated

Re: Yikes! String is mutable! (was Re: [groovy-user] XMLParser: attribute gets truncated when updated)

by Jim White :: Rate this Message:

Reply to Author | View in Thread

Gael Marziou wrote:

> Thanks for the somehow frightening explanations, it helped me to find a
> workaround.
>
> I changed the buggy line :
>
> tag.attribute("id").value = "ABCD"
>
> to this:
>
> tag.attributes().id = "ABCD"
>
> Is there a better way?

That looks good to me.  You're getting the map of attributes and setting
the element 'id' to a new value.  It is equivalent to this (less
pleasing) statement:

tag.attributes()['id'] = "ABCD"

Ah, and you've jogged my memory (I don't use this Groovy XML/DOM jazz
much).  There are GPath expressions.  So you can also do this:

tag.'@id' = "ABCD"

or even:

tag.@id = "ABCD"

Although the wiki says that is for when using XmlSlurper, and so I'm not
sure when you can't use it.  There are some differences when using W3C
DOM although they can be adjusted for by using a category.

http://groovy.codehaus.org/GPath

http://groovy.codehaus.org/Processing+XML

More details in the GinA (Groovy in Action) book, which I recommend.

Jim


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: XMLParser: attribute gets truncated when updated