简体   繁体   English

使用Groovy脚本读取基于其他标签的XML标签值

[英]Read XML tag values based on other tags using Groovy Script

I'm new to Groovy scripting and I need some help in fetching tag values based on different tag. 我是Groovy脚本的新手,在基于不同标签获取标签值时需要一些帮助。 Below is the response I'm receiving from my application through SOAP. 以下是我通过SOAP从应用程序收到的响应。

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
  <DataResponse xmlns="http://eh.dummy.com">
     <onlinePartyData xmlns="">
        <tabs xmlns="http://eh.dummy.com">
           <tabs_InnerSet>
              <Tabs_InnerSet_TupleType>
                 <tab>
                    <DISPLAY_TEXT>DESCRIPTION 1</DISPLAY_TEXT>
                    <qsets>
                       <qsets_InnerSet>
                          <Qsets_InnerSet_TupleType>
                             <qset>
                                <SET_CD>QS1</SET_CD>
                                <VISIBLE>true</VISIBLE>
                             </qset>
                          </Qsets_InnerSet_TupleType>
                          <Qsets_InnerSet_TupleType>
                             <qset>
                                <SET_CD>QS2</SET_CD>
                                <VISIBLE>true</VISIBLE>
                             </qset>
                             <qset>
                                <SET_CD>QS3</SET_CD>
                                <VISIBLE>false</VISIBLE>
                             </qset>
                          </Qsets_InnerSet_TupleType>

                       </qsets_InnerSet>
                    </qsets>
                 </tab>
              </Tabs_InnerSet_TupleType>
           </tabs_InnerSet>
        </tabs>
     </onlinePartyData>
  </DataResponse>
</soap:Body>
</soap:Envelope>

I need SET_CD tag values to be concatenated if VISIBLE node is true. 如果VISIBLE节点为true,则需要串联SET_CD标记值。 In this example, I need output of my groovy script as QS1, QS2. 在此示例中,我需要将Groovy脚本的输出作为QS1,QS2。 How can I achieve this? 我该如何实现?

Thanks!! 谢谢!!

Here is the script that queries the xml where VISIBLE = true and fetches the SET_CD values and joins them. 这是查询XML的脚本,其中VISIBLE = true并获取SET_CD值并将其联接。

Note that the xml provided in the question is not well-formed. 请注意,问题中提供的xml格式不正确。 Fixed that while testing. 在测试时修复了该问题。

Groovy Script Groovy脚本

//Pass the xml string to parseText method
def parsedXml = new XmlSlurper().parseText(xml)
def result = parsedXml.'**'.findAll {it.VISIBLE == true}.SET_CD.join(',')
println result
assert result instanceof String 

If you want to try this quickly online Demo 如果您想快速尝试在线演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM