Groovy:访问Closure对象';s元素

Groovy:访问Closure对象';s元素,groovy,Groovy,我是Groovy的新手,我想知道: 如果我这样定义一个对象: def buildParentXML(){ def parentXMLElement = { ParentElement { CreationDate(new Date()) out << buildChildXML() ChildElementFK(buildChildXML().childElement.Ch

我是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")
     }
  }
简单示例

<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>

AK
添加
爱
09/17/2013
09/17/2013
09/17/2013
09/17/2013
09/17/2013
09/17/2013
09/17/2013
250
将转换为

<?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>

政策
提交
不适用
保存
AK
DD
爱
09/17/2013
09/17/2013
09/17/2013
09/17/2013
09/17/2013
09/17/2013
09/17/2013
01
250
-123464646

对,我胡乱猜了一下……这就是你的意思吗

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>

2013年9月16日星期一17:02:42英国夏令时
12345679
不知道
12345679

我不相信这是可能的,是吗?那太糟糕了。我用它来构建XML层,有时我需要将一些值从子XML传递到父XML。你能给出一个正确的例子来说明你是如何做到这一点的吗?我能跑什么?您应该能够将要筛选的值返回给父级,但是如果没有一个可运行的示例,很难说…--我应该将最后的注释量化为一个适当的小示例;-)有帮助吗?在我的例子中,这些子XML至少有2-3层父子关系。这接近我想要的,但我不理解委托部分。如果父母有一个PK,而祖父母将父母的PK作为FK,这会起作用吗?去吃午饭。30分钟后回来。@HimanshuYadav,我不知道你的意思。我怀疑有一种比将XML生成拆分为多个单独的闭包更简单的方法,事实上,如果XML是标准格式,那么它可能是一种更简单的方法,而不是采用MarkupBuilder路线。我知道你是对的。这是translator类的第一步,它将UIRequst xml转换为遗留应用程序的请求xml。甚至我也不知道这个XML可以改变多少。这就是为什么我试图将其分解为多个闭包对象。您可能在为自己做不必要的工作。此外,我仍然不确定您要做什么(没有端到端的示例);-)
<?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>