簡體   English   中英

Groovy:訪問Closure對象的元素

[英]Groovy: Accessing Closure Object's elements

我是Groovy的新手,想知道:

如果我定義這樣的對象:

 def buildParentXML(){
        def parentXMLElement = {
           ParentElement {
            CreationDate(new Date())
            out << buildChildXML()
            ChildElementFK(buildChildXML().childElement.ChildPK) //Something like this
           }
        }
      }
   def buildChildXML() {
     def childElement {
        ChildPK("12345679")
        Operator("Don't Know")
     }
  }

我將如何訪問Element1Element2的值?

我試過了

println obj.RootElement.Element1
println obj[RootElement].[Element1]
println obj['RootElement'].['Element1']

簡單的例子

<SavePolicy>
<Segment>
    <IssueState>AK</IssueState>
    <OptionCode>ADD</OptionCode>
    <SegmentStatus>Aive</SegmentStatus>
    <ApplicationReceivedDate>09/17/2013</ApplicationReceivedDate>
    <ApplicationSignedDate>09/17/2013</ApplicationSignedDate>
    <CreationDate>09/17/2013</CreationDate>
    <EffeiveDate>09/17/2013</EffeiveDate>
    <IssueDate>09/17/2013</IssueDate>
    <TerminationDate>09/17/2013</TerminationDate>
    <RateSeriesDate>09/17/2013</RateSeriesDate>
</Segment>
<Life>
    <FaceAmount>250.00</FaceAmount>
</Life>

將被轉換成

<?xml version="1.0" encoding="UTF-8"?>
<SEGRequestVO>
    <Service>Policy</Service>
    <Operation>submit</Operation>
    <Operator>N/A</Operator>
    <IgnoreEditWarningsNF/>
    <RequestParameters>
        <SubmissionType>SaveIt</SubmissionType>
        <ContraNumber/>
        <SegmentVO>
            <IssueState>AK</IssueState>
            <OptionCode>DD</OptionCode>
            <SegmentStatus>Aive</SegmentStatus>
            <ApplicationReceivedDate>09/17/2013</ApplicationReceivedDate>
            <ApplicationSignedDate>09/17/2013</ApplicationSignedDate>
            <CreationDate>09/17/2013</CreationDate>
            <EffeiveDate>09/17/2013</EffeiveDate>
            <IssueDate>09/17/2013</IssueDate>
            <TerminationDate>09/17/2013</TerminationDate>
            <RateSeriesDate>09/17/2013</RateSeriesDate>
            <ContraNumber/>
            <ProduStruureFK>01</ProduStruureFK>
            <LifeVO>
                <FaceAmount>250.00</FaceAmount>
                <LifePK>-123464646</LifePK>
                <SegmentFK/>
            </LifeVO></SegmentVO>
        </RequestParameters>
    </SEGRequestVO>

是的,我大膽猜測...這是您的意思嗎?

import groovy.xml.*

def buildChildXML = {
    ChildPK("12345679")
    Operator("Don't Know")

    return "12345679"
}

def buildParentXML = {
    ParentElement {
        CreationDate(new Date())
        def pk = buildChildXML()
        ChildElementFK( pk )
    }
}

println XmlUtil.serialize( new StreamingMarkupBuilder().bind { it ->
    buildParentXML.delegate = it
    buildChildXML.delegate = it
    buildParentXML()
} )

打印:

<?xml version="1.0" encoding="UTF-8"?><ParentElement>
  <CreationDate>Mon Sep 16 17:02:42 BST 2013</CreationDate>
  <ChildPK>12345679</ChildPK>
  <Operator>Don't Know</Operator>
  <ChildElementFK>12345679</ChildElementFK>
</ParentElement>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM