Grails: Store XML values in database
Is there any way to store some properties of domain object in database instead of storing them as strings?
i.e.
class Document
{
String name
Node 开发者_JAVA百科value
}
instead of:
class Document
{
String name
String value
}
Here is an example of reading from XML and storing the values in database using grails
Employee info xml file:
<employees>
<employee>
<firstname>Marios</firstname>
<deptname>ITdepartment</deptname>
<empid>123</empid>
</employee>
<employee>
<firstname>Ben</firstname>
<deptname>Management</deptname>
<empid>124</empid>
</employee>
</employees>
Read and save in Employee table :
def Employee = new XmlParser().parse("Your Xml file path")
def set1 = sql.dataSet("Your field name in Xml")
Employee.employee.each {
def firstname = it.firstname.text()
def deptname = it.deptname.text()
def empid = it.empid.text()
set1.add(first_name:firstname,dept_name:deptname,emp_id:empid)
}
精彩评论