groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul King <pa...@asert.com.au>
Subject Re: Add Namespace to the XML attribute
Date Tue, 05 Jul 2016 02:50:46 GMT
Just as a comment, you should consider asking such questions on the
users mailing list. The dev list is all about development of Groovy
itself.

In any case, your problem can probably be solved with XmlSlurper but
with more complexity than is ideal. I would use XmlParser:

def response = new XmlParser().parseText(books)
response.'**'.findAll{ it.@available }.each {
  it.'@bk:isavailable' = 'true'
}
println groovy.xml.XmlUtil.serialize(response)

or if you really do need to cater for available being the empty string, then:

def response = new XmlParser().parseText(books)
response.'**'.findAll{ it.@available != null }.each {
  it.'@bk:isavailable' = 'true'
}
println groovy.xml.XmlUtil.serialize(response)


On Tue, Jul 5, 2016 at 3:01 AM, GroovyBeginner <groovybeginner@gmail.com> wrote:
> Hi,
> I'm trying to accomplish adding the namespace to the attribute . Is there a
> way to do this?Here is the XML structure.
>  <?xml version="1.0" encoding="UTF-8"?><bk:Books
> xmlns:bk="urn:com.books/findbooks">
>   <bk:book bk:available="20" isavailable="1" bk:id="1">
>     <bk:title isavailable="0">Don Xijote</bk:title>
>     <bk:author bk:available="20" isavailable="1" bk:id="1">Manuel De
> Cervantes</bk:author>
>   </bk:book>
> </bk:Books>
>
> My Output should get the following:
>
>  <?xml version="1.0" encoding="UTF-8"?><bk:Books
> xmlns:bk="urn:com.books/findbooks">
>   <bk:book bk:available="20" bk:isavailable="1" bk:id="1">
>     <bk:title bk:isavailable="0">Don Xijote</bk:title>
>     <bk:author bk:available="20" bk:isavailable="1" bk:id="1">Manuel De
> Cervantes</bk:author>
>   </bk:book>
> </bk:Books>
>
>
>
>
> --
> View this message in context: http://groovy.329449.n5.nabble.com/Add-Namespace-to-the-XML-attribute-tp5733765.html
> Sent from the Groovy Dev mailing list archive at Nabble.com.

Mime
View raw message