使用groovy在SoapUI中解析XML响应

时间:2014-04-01 16:31:28

标签: xml parsing groovy soapui

我想在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>

2 个答案:

答案 0 :(得分:3)

dmahapatro&#39; answer is correct基于您原始提供的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)代码返回一个节点和definition of a node states

  

表示可用于结构化的任意树节点   元数据或任何类似XML的树。节点可以有一个名称,a   值和可选的属性映射

如果你仔细观察你得到的是一个带有名称,值和可选的属性图的节点,这是一个很好的例子。

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

答案 1 :(得分:2)

应该是

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

注意:RoomStays是根,未使用,因为已解析的xml设置为该节点。