我是一个 grails 新手,在一个有趣的项目上工作。我正在序列化这样的类:
def msg = (listing as XML).toString()
尝试使用 XMLSlurper 反序列化一个类,如下所示:
def root = new XmlSlurper().parseText(listingString)
def sellerNode = root.seller
我正在尝试重建这个对象:
Listing{
Date dateCreated
String description
Date endDateTime
String name
Float startingPrice
Customer winner
static hasMany = [bids: Bid] // B-4
static belongsTo = [seller: Customer]
}
}
从这个xml:
<?xml version="1.0" encoding="UTF-8"?>
<listing>
<bids>
<bid>
<amount>10.5</amount>
<bidder>
<accountExpired>false</accountExpired>
<accountLocked>false</accountLocked>
<dateCreated/>
<emailAddress>validguy@valid.com</emailAddress>
<enabled>false</enabled>
<password>secret</password>
<passwordExpired>false</passwordExpired>
<username>validguy</username>
</bidder>
<dateCreated>2012-04-08 21:16:41.423 CDT</dateCreated>
<listing/>
</bid>
<bid>
<amount>10.0</amount>
<bidder>
<accountExpired>false</accountExpired>
<accountLocked>false</accountLocked>
<dateCreated/>
<emailAddress>validguy@valid.com</emailAddress>
<enabled>false</enabled>
<password>secret</password>
<passwordExpired>false</passwordExpired>
<username>validguy</username>
</bidder>
<dateCreated>2012-04-08 21:16:41.415 CDT</dateCreated>
<listing/>
</bid>
</bids>
<dateCreated/>
<description>A test listing</description>
<endDateTime>2012-04-09 21:16:41.407 CDT</endDateTime>
<name>Default</name>
<seller>
<accountExpired>false</accountExpired>
<accountLocked>false</accountLocked>
<dateCreated/>
<emailAddress>validguy@valid.com</emailAddress>
<enabled>false</enabled>
<password>secret</password>
<passwordExpired>false</passwordExpired>
<username>validguy</username>
</seller>
<startingPrice>10.0</startingPrice>
<wasNotificationSent>false</wasNotificationSent>
<winner>
<accountExpired>false</accountExpired>
<accountLocked>false</accountLocked>
<dateCreated/>
<emailAddress>validguy@valid.com</emailAddress>
<enabled>false</enabled>
<password>secret</password>
<passwordExpired>false</passwordExpired>
<username>validguy</username>
</winner>
</listing>
首先,我在获取每个节点的值时遇到问题。我试过 def Seller = new Customer(name:sellerNode.@username) 但这不起作用,因为我认为 @username 需要是一个属性而不是一个元素。其次,我是否必须“手动”解析这个 xml?难道没有更好的方法来自动反序列化这个xml吗?
我已经看过几篇文章,包括这篇文章:将 XML 导入 Grails 域类 ,但是如您所见,我的 xml 没有像这篇文章中的 xml 这样的属性。
谢谢,