Groovy to XML and viceversa

georgeajin

New member
Hi all,

Is it possible to convert a groovy pojo class into XML and viceversa using XML Converter in Grails. If so it would be very helpful if anyone could provide an example....:)



Regards

Ajin
 

dave

Administrator
Staff member
Sure you can use render any object as XML, try it out and see how you find the results.. e.g.

render someInstance as XML

Usually thought, I end up constructing my own XML as I don't like the autogenerated stuff....

Have a play around with the 'as' keyword.

def myXML = anObjectInstance as XML

then try converting back passing the XML into a constructer, e.g.

def newObject = new YourDomainClass(myXML)


Read up on Grails Converters : http://grails.org/Converters+Reference
 

georgeajin

New member
Hi dave,

I tried that way, but its throwing exception as given below:-

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'app.Post@c0e844' with class 'app.Post' to class 'grails.converters.XML'
at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:340)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.asType(DefaultGroovyMethods.java:11584)


at this line
def xml1 = post as XML;


Regards

Ajin
 
Top