views:

125

answers:

1

I have a grails project that contains a few domain objects. I am using a java project in this code which can parse a document for me. The controller that calls that Java project is using JAXB to generate XML from the object returned by the Java project. I want to use this XML document (which is generated after some text parsing, using JAXB) to populate my Domain classes in my grails project. How does this work in grails? Can I use something like Castor, and create a mapping using the names of my groovy classes? The idea is I want to generate new entries in the database and save it for the user based on whatever text was parsed out of the document they uploaded.

How does this even work in grails anyway? Can I create a new Domain object from another object's controller with something like

 Project p = new Project(); 

and then do a p.save()?

A: 

Download the Castor Core and Castor XML jars from here and put them in the lib directory (there's probably a better way to manage this dependency using Grails' dependency management, but this one's a quick and dirty).

With Castor introspection mode you don't need to worry about creating mapping files if your XML matches up nicely with your domains. Here's an example:

grails-app/domain/MyDomain.groovy

class MyDomain {
    String foo
    String bar
}

grails-app/controllers/MyController.groovy

import org.exolab.castor.xml.Unmarshaller
import java.io.ByteArrayInputStream

class MyController {

    def myAction = {
        def xml = '''
<myDomain>
  <foo>My Foo String</foo>
  <bar>My Bar String</bar>
</myDomain>
'''
        def reader = new ByteArrayInputStream(xml.bytes).newReader()
        def domain = (MyDomain)Unmarshaller.unmarshal(MyDomain.class, reader)
        domain.save()

        def count = MyDomain.countByFoo('My Foo String')

        render "Found $count results"
    }
}

Navigate to localhost:8080/appname/my/myAction and it should display "Found N results", N > 0.

Rob Hruska
Based on reading about the introspection - lets say my XML file has a lot of stuff in it - will this be smart enough to see that since I am casting to (MyDomain) type for the unmarshaller, that it will dig through the XML file to find those objects?
Derek
Can you give an example of what your XML would be? That might help me understand your intentions a little better.
Rob Hruska
http://pastebin.com/8Qb1ewdM Here is a pastebin of a sample output. The domain classes would be Education, Employer, Project, Tool, Skill. If it is an embedded classtype here, such as project, then Employer holds a list of those Objects, that way the right project is linked to the right employer
Derek
Hmm, a little more complex than I thought. I don't have time to work up an answer that will meet your input exactly, but I think my answer in its current form will give you a start. You can use Groovy's XmlSlurper to grab portions of that XML and then use Castor for each piece to create domains.
Rob Hruska
Thanks, I am using it as a start, and doing a little further googling too
Derek