2014-06-14 28 views
1

我收到如下回應:如何使用腳本斷言提取CDATA?

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
     <GetUKLocationByPostCodeResponse xmlns="http://www.webserviceX.NET"> 
     <GetUKLocationByPostCodeResult><![CDATA[<NewDataSet> 
    <Table> 
    <Town>Grangetown</Town> 
    <County>Cardiff</County> 
    <PostCode>CF1</PostCode> 
    </Table> 
    <Table> 
    <Town>Leckwith</Town> 
    <County>Vale of Glamorgan</County> 
    <PostCode>CF1</PostCode> 
    </Table> 
    <Table> 
    <Town>Canton</Town> 
    <County>Cardiff</County> 
    <PostCode>CF1</PostCode> 
    </Table> 
    <Table> 
    <Town>Cardiff</Town> 
    <County>Cardiff</County> 
    <PostCode>CF1</PostCode> 
    </Table> 
</NewDataSet>]]></GetUKLocationByPostCodeResult> 
     </GetUKLocationByPostCodeResponse> 
    </soap:Body> 
</soap:Envelope> 

我能夠提取的鎮,縣,郵編使用屬性轉移..

但是,如何提取使用相同的腳本斷言..

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context); 
log.info(groovyUtils) 
def holder = groovyUtils.getXmlHolder("GetUKLocationByPostCode#ResponseAsXml") 
log.info(holder) 
def num = holder.getNodeValue("//Town") 
log.info(num) 

它返回意外的元素cdata ..如何解決這個..?

+0

你嘗試的文件? http://www.soapui.org/Functional-Testing/working-with-cdata.html – SiKing

回答

3

我通過下面的腳本實現了這個:

import com.eviware.soapui.support.XmlHolder 
responsexmlholder = new XmlHolder(messageExchange.getResponseContentAsXml()) 
responsexmlholder.declareNamespace("ns1","http://www.webserviceX.NET") 
Cdataxml = responsexmlholder.getNodeValue("//ns1:GetUKLocationByPostCodeResult") 
Cdataxmlholder = new XmlHolder(Cdataxml) 
Town = Cdataxmlholder.getNodeValue("//Town") 
+0

只需要注意一點:通常您不必亂用名稱空間,因此您可以執行諸如'responsexmlholder.getNodeValue(「 // *:GetUKLocationByPostCodeResult「)'並跳過整個'responsexmlholder.declareNamespace(...)'業務。 – SiKing

+1

是的,這是有道理的:) – ChanGan