开发者

Grails: Store XML values in database

开发者 https://www.devze.com 2023-02-12 07:33 出处:网络
Is there any way to store some properties of domain object in database instead of storing them as strings?

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)
}
0

精彩评论

暂无评论...
验证码 换一张
取 消