Reputation: 33992
I am trying to convert JSON to XML in Grails via:
import net.sf.json.JSONObject
import net.sf.json.xml.XMLSerializer
...
String convertToXmlString(jsonString) {
JSONObject json = JSONObject.fromObject(jsonString)
String xml = new XMLSerializer().write( json )
return xml
}
but when I try to run the code I get the exception:
Caused by: java.lang.ClassNotFoundException: nu.xom.Serializer
at org.codehaus.groovy.tools.RootLoader.findClass(RootLoader.java:156)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at org.codehaus.groovy.tools.RootLoader.loadClass(RootLoader.java:128)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I haven't run into something like this before. Aren't I importing all of the necessary classes? I have imported the json-lib jar into my /lib
folder, and run grails compile --refresh-dependencies
. Is there another step I need to run?
I'm open to other ways of converting JSON/maps -> XML/strings and back, but this issue seems like it should be easily fixed.
Upvotes: 1
Views: 2602
Reputation: 680
You can use the grails.converters to accomplish this:
import grails.converters.JSON
import grails.converters.XML
def o = JSON.parse("{ foo: 'bar' }") // creates a JSONObject
def x = o as XML // creates an XML
println x
def s = x as String // create the String representing the XML
println s
Result will be like:
<?xml version="1.0" encoding="UTF-8"?><map><entry key="foo">bar</entry></map>
<?xml version="1.0" encoding="UTF-8"?><map><entry key="foo">bar</entry></map>
Upvotes: 1