开发者

XML parsing in Grails

I am newbie to grails. I want to save some data using REST.

I will get the xml from the request.XML. It has child nodes and the child nodes also have the child node..

For ex:

<?xml version="1.0" encoding="UTF-8"?>

<contract id="1">

    <startDate>2011-09-11</startDate>

    <orderType />

    <charges>

        <charge id="3">

            <Code>MO-AV-SP-2008</Code>

            <position>1</position>

            <isPaid>false</isPaid>

            <isPenalty>false</isPenalty>

            <billerGroupCode />

            <pricings>

                <pricing id="7">

                    <unitsTo />

                    <percent />

                    <isOverage>false</isOverage>

                    <contractCharge id="2" />

                    <lastUpdated>2011-09-11</lastUpdated>

                    <currency id="USD" />

                    <price>100.00</price>

                    <dateCreated>2011-09-11</dateCreated>

                    <unitsFrom />

                </pricing >

            </pricings>

            <isProrated>false</isProrated>

            <unitOfMeasure />

            <priceCode>SNGL-SETUP</priceCode>

            <invoiceText>Setup fee</invoiceText>

            <pricingType>FixedPricing</pricingType>

            <lastUpdated>2011-09-11</lastUpdated>

            <standardQuantity />

            <isTax>false</isTax>

            <maxQuantity />

            <taxCodes />

            <isMandatory>false</isMandatory>

            <dateCreated>2011-09-11</dateCreated>

            <isSeparateInvoice>false</isSeparateInvoice>

            <chargeType>OneTimeCharge</chargeType>

            <notes>Setup Fee</notes>

            <minQuantity />

        </charge>

    </charges>

    <dueDateValue>1</dueDateValue>

    <invoiceText>Monthly</invoiceText>

    <lastUpdated>2011-10-10</lastUpdated>

    <endDate>2012-09-10</endDate>

    <interestRate />

    <fixedChargesReducePayoff />

    <billingPeriod>Monthly</billingPeriod>

    <name>Anti Virus</name>

    <isAutoRenew>false</isAutoRenew>

    <dateCreated>2011-09-11</dateCreated>

    <notes>Pay monthly for Anti Virus</notes>

    <numberOfInstallments />

    <product id="3" />

    <contractCode>B-Mo-AV-SP</contractCode>

    <dueDateUnits>Months&开发者_运维百科lt;/dueDateUnits>

    <billingAlignment>StartDate</billingAlignment>

</contract>

From the above xml I want to save all datas to database as a new data, not with the same id. I want to bind the the data to particular table.

Thanks in advance..

Nimmy


Use XMLparser or XMLSlurper. Here are examples showing how to use each. These classes will only help you to bind the XML data to objects, you'll have to deal with the persistence yourself.

If the data is bound to Grails domain objects, then persistence is probably not much more complicated than calling save() on each one.


You can use dot notation to traverse your object (if you don't care about attribute based selectors). So like this grandParentNode.parentNode.childNode makes sense?

Here is a great example: http://groovy.codehaus.org/Reading+XML+using+Groovy's+XmlSlurper


you can use this as a reference. You can parse your xml like this.

    def a = "YOUR XML DATA"
    def contract = new XmlSlurper().parseText(a)

    println contract.@'id'
    println contract.startDate

   contract.pricings.each(){
      println it.@'id'
      println it.isOverage
   }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜