0

我是 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")
     }
  }

我将如何访问Element1or的值Element2

我试过了

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

1 回答 1

0

对了,我胡乱猜测了……你是这个意思吗?

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-09-16T16:03:46.900 回答