0

我有以下 XML,在 SOAPUI Groovy 中,我尝试去捕获一组带有标签和值的 XML,例如:

<telephoneNumbers>
    <telephone>
        <id>125042741</id>
        <areaCode>0161</areaCode>
        <phoneNumber>4804420</phoneNumber>
        <extension>1234</extension>
        <usage>Work</usage>
    </telephone>
</telephoneNumbers>

我试图返回以下结果(标签和值):

<telephone>
    <id>125042741</id>
    <areaCode>0161</areaCode>
    <phoneNumber>4804420</phoneNumber>
    <extension>1234</extension>
    <usage>Work</usage>
</telephone>

这是时髦的:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )  
def Recall = groovyUtils.getXmlHolder( "Recall#Response" ) 

def telephone = Recall[ "//telephone//*" ] as String
String returnXml = ""

if ( Recall["//restrict"] !=  null ) {
    returnXml= telephone
}
else 
    return returnXml
4

1 回答 1

0

Similar to this answer How do I print a groovy Node with namespace preserved? you could parse the Xml using XmlSlurper and afterwards print the xml nodes you wish to with StreamingMarkupBuilder.

String xml = Recall.getXml()
def telephoneNumbers = new XmlSlurper().parseText(xml)​​​​​​​​
def outputBuilder = new groovy.xml.StreamingMarkupBuilder()
String telephoneXml = outputBuilder.bind { mkp.yield telephoneNumbers.telephone }

I posted a runnable example here: http://groovyconsole.appspot.com/script/1245001 not using SOAPUI but a simple string for demonstration. Hope this helps!

于 2013-10-25T21:22:04.787 回答