当前位置 : 主页 > 网页制作 > xml >

在Grails中将xml转换为域对象

来源:互联网 收集:自由互联 发布时间:2021-06-13
我是一个有趣的项目的grails新手.我正在序列化这样一个类: def msg = (listing as XML).toString() 尝试使用XMLSlurper反序列化类,如下所示: def root = new XmlSlurper().parseText(listingString)def sellerNode =
我是一个有趣的项目的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卖家=新客户(名称:sellerNode.@ username),但这不起作用,因为我认为@username需要是属性而不是元素.
其次,我必须“手动”解析这个xml吗?是不是有更好的方法来自动反序列化这个xml?

我已经看了几个帖子,包括这个帖子:Import XML into a Grails Domain Class然而你可以看到,我的xml没有像这篇文章中的xml这样的属性.

谢谢,

如果XML的源是Web请求,则可以将parseRequest:true添加到控制器的UrlMapping,grails将自动解析XML请求. XML将以params的形式呈现,您可以像任何其他请求一样进行数据绑定.

如果您从其他源获取XML,请查看类XMLParsingParameterCreationListener.groovy中的XML到参数映射转换.

网友评论