2014-04-01 35 views
2

我想在SoapUI中解析XML响应。我有下面的脚本,但不知何故我无法解析。你能帮我完善代码吗?使用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> 

回答

2

应该

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

注:RoomStays是根,而不是使用,因为解析XML设置到该节点。

+0

谢谢你,但是我得到空的输出。你能否看看下面更新的评论 – user3463885

3

dmahapatroanswer 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的树的任意树节点。一个节点可以有一个名字,一个 值和一个可选的地图属性

如果你仔细观察,你得到了什么是名称,值和属性的可选地图,一个很好的例子节点的..

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