2015-11-05 63 views
0

我有下面的代碼運行良好,並基於前一個請求的響應構建請求。我現在需要做的就是在條件允許的情況下構建節點,如果問題存在。有異常是真的。 爲真。 如何在下面的代碼中包含if條件以僅包含標記設置爲true的節點?SoapUI -Groovy XML.Markupbuilder如果條件

root = new XmlSlurper().parseText(xml) 

def sw = new StringWriter() 
def request = new groovy.xml.MarkupBuilder(sw) 

request.UpdateTargetGroupList{ 
     root.Body.GetQualityIndicatorsResponse.GetQualityIndicatorsOutput.QIDetails.GroupList.Group.each{group-> 
//Group{ 
    ID(group.ID) 
    RowVersion(group.RowVersion) 
    QuestionList{ 
    group.QuestionList.Question.each{question-> 
    Question(){ 
     ID(question.ID) 
     RowVersion(question.RowVersion) 
     QuestionType(question.QuestionType) 
     TargetValue(question.TargetValue) 

    } 
    } 
} 
} 
} 
} 

log.info sw 
+0

你有機會看看解決方案嗎? – Rao

回答

1

使用您的示例代碼來演示如何有條件添加節點。看下面的例子。

引入布爾變量isTargetValue並分配爲true。當然,您可以根據需要將它用於任何元素。

def isTargetValue = true 
root = new XmlSlurper().parseText(xml) 
def sw = new StringWriter() 
def request = new groovy.xml.MarkupBuilder(sw) 

request.UpdateTargetGroupList { 
    root.Body.GetQualityIndicatorsResponse.GetQualityIndicatorsOutput.QIDetails.GroupList.Group.each { group-> 
     ID(group.ID) 
     RowVersion(group.RowVersion) 
     QuestionList { 
      group.QuestionList.Question.each { question-> 
       Question(){ 
        ID(question.ID) 
        RowVersion(question.RowVersion) 
        QuestionType(question.QuestionType) 
        if (isTargetValue) { 
         TargetValue(question.TargetValue) 
        } 
       } 
      } 
     } 
    } 
} 

log.info sw 

希望這會有所幫助。