簡體   English   中英

使用Groovy在SoapUI中解析XML響應

[英]Parse XML response in SoapUI using groovy

我想在SoapUI中解析XML響應。 我有以下腳本,但是以某種方式我無法解析。 誰能幫助我完善代碼。

def response = context.expand( '${WS_01_Hotel_Search#Response#declare namespace soap=\'http://www.w3.org/2003/05/soap-envelope\'; //OTA_HotelAvailRS[1]/RoomStays[1]}' )
def responseParser = new XmlParser().parseText(response)

responseParser.RoomStays.RoomStay.RoomTypes.RoomType.each { RoomType ->
   log.info("${RoomType.'@RoomTypeCode'} ${RoomType.'@RoomID'}");
}

XML代碼:

<RoomStays>
    <RoomStay>
       <RoomTypes>
          <RoomType RoomTypeCode="AAA" RoomID="BB" Quantity="9">
             <RoomDescription>
                <Text>Double Room</Text>
             </RoomDescription>
          </RoomType>
          <RoomType RoomTypeCode="BBB" RoomID="CC" Quantity="9">
             <RoomDescription>
                <Text>Double Room</Text>
             </RoomDescription>
          </RoomType>
          <RoomType RoomTypeCode="CCC" RoomID="DD" Quantity="9">
             <RoomDescription>
                <Text>Executive Family Room</Text>
             </RoomDescription>
          </RoomType>
          <RoomType RoomTypeCode="DDD" RoomID="EE" Quantity="9">
             <RoomDescription>
                <Text>Executive Family Room</Text>
             </RoomDescription>
          </RoomType>
       </RoomTypes>                             
    </RoomStay>
 </RoomStays>

dmahapatro答案基於您原來提供的xml 是正確的。 解析的xml設置為根節點。 如果您以前提供了原始的xml以及作為響應得到的內容,那將真的有幫助。

您還問了為什么

{http://www.w3.org/2003/05/soap-envelope}Envelope[attributes={}; value=[{http://www.w3.org/2003/05/soap-envelope}Body[attributes={}; value=[{http://www.opentravel.org/OTA/2003/05}OTA_HotelAvailRS[attributes={TimeStamp=2014-04-01T16:09:25, Target=Production, Version=1}; value=[{http://www.opentravel.org/OTA/2003/05}Success[attributes={}; value=[]], {http://www.opentravel.org/OTA/2003/05}RoomStays[attributes={}; value=[{http://www.opentravel.org/OTA/2003/05}RoomStay[attributes={}; value=[{http://www.opentravel.org/OTA/2003/05}RoomTypes[attributes={}; value=[{http://www.opentravel.org/OTA/2003/05}RoomType[attributes={RoomTypeCode=AAA, RoomID=AA, Quantity=9}; value=[{http://www.opentravel.org/OTA/2003/05}RoomDescription[attributes={}; value=[{http://www.opentravel.org/OTA/2003/05}Text[attributes={}; value=[Double Room - Room only]]]]]], {http://www.opentravel.org/OTA/2003/05}RoomType[attributes={RoomTypeCode=BBB, RoomID=BB, Quantity=9}; value=[{http://www.opentravel.org/OTA/2003/05}RoomDescription[attributes={}; value=[{http://www.opentravel.org/OTA/2003/05}Text[attributes={}; value=[Double Room - Bed and Breakfast]]]]]], {http://www.opentravel.org/OTA/2003/05}RoomType[attributes={RoomTypeCode=CCC, RoomID=CC, Quantity=9}; value=[{http://www.opentravel.org/OTA/2003/05}RoomDescription[attributes={}; value=[{http://www.opentravel.org/OTA/2003/05}Text[attributes={}; value=[Executive Family Room - Room only]]]]]], {http://www.opentravel.org/OTA/2003/05}RoomType[attributes={RoomTypeCode=DDD, RoomID=DD, Quantity=9}; value=[{http://www.opentravel.org/OTA/2003/05}RoomDescription[attributes={}; value=[{http://www.opentravel.org/OTA/2003/05}Text[attributes={}; value=[Executive Family Room - Bed and Breakfast]]]]]]]], {http://www.opentravel.org/OTA/2003/05}GuestCounts[attributes={}; value=[{http://www.opentravel.org/OTA/2003/05}GuestCount[attributes={AgeQualifyingCode=10, Count=2}; value=[]]]], {http://www.opentravel.org/OTA/2003/05}TimeSpan[attributes={Start=2014-06-29, Duration=P3N}; value=[]], {http://www.opentravel.org/OTA/2003/05}BasicPropertyInfo[attributes={ChainCode=GDF, HotelCode=1234, HotelName=ABC Resort}; value=[]], {http://www.opentravel.org/OTA/2003/05}TPA_Extensions[attributes={}; value=[{http://www.opentravel.org/OTA/2003/05}SortOrder[attributes={}; value=[1]]]]]]]]]]]]]]

parser.parseText (response)代碼返回節點和節點狀態定義

表示一個可用於結構化元數據或任何類似XML的樹的任意樹節點。 節點可以具有名稱,值和可選的屬性映射

如果仔細觀察,您會發現一個帶有名稱,值和可選屬性映射的節點,這是一個很好的例子。

RoomType[attributes={RoomTypeCode=BBB, RoomID=BB, Quantity=9}; value=[{http://www.opentravel.org/OTA/2003/05}RoomDescription[attributes={}; value=[{http://www.opentravel.org/OTA/2003/05}Text[attributes={}; value=[Double Room - Bed and Breakfast]

代表xml的以下部分。

<RoomType RoomTypeCode="BBB" RoomID="BB" Quantity="9">
    <RoomDescription>
        <Text>Double Room - Bed and Breakfast</Text>
    </RoomDescription>
</RoomType>

根據您的原始xml,嘗試以下代碼訪問每種房型

//I put your xml in a test request step as a request, this code will work even if it is in the response.
def response = context.expand('${Test Request#Request}')

//the xml uses namespaces so they have to be declared
def soap = new groovy.xml.Namespace("http://www.w3.org/2003/05/soap-envelope", 'soap')
def ns = new groovy.xml.Namespace("http://www.opentravel.org/OTA/2003/05", 'ns')

XmlParser parser = new XmlParser()
def root = parser.parseText (response)

def rt = root[soap.Body][ns.OTA_HotelAvailRS][ns.RoomStays][ns.RoomStay][ns.RoomTypes][ns.RoomType]

assert rt != null

rt.each { RoomType ->
    log.info "${RoomType.'@RoomTypeCode'} ${RoomType.'@RoomID'}"
}

這給我

Wed Apr 02 10:13:34 ADT 2014:INFO:AAA AA
Wed Apr 02 10:13:34 ADT 2014:INFO:BBB BB
Wed Apr 02 10:13:34 ADT 2014:INFO:CCC CC
Wed Apr 02 10:13:34 ADT 2014:INFO:DDD DD

應該

responseParser.RoomStay.RoomTypes.RoomType.each { RoomType ->
   log.info "${RoomType.'@RoomTypeCode'} ${RoomType.'@RoomID'}"
}

注意: RoomStays是根節點,不被使用,因為已解析的xml設置為該節點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM