2016-05-12 6 views
0

APIからxml SOAPレスポンスを取得しています。私はAPIのURLをヒットするために "要求"ライブラリを使用しています。文字列の形式で必要なXMLメッセージを抽出します。

SOAPレスポンス:今すぐ

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
<SOAP-ENV:Header/> 
<SOAP-ENV:Body> 
    <ns2:AvailabilityResponse xmlns:ns2="http://www.derbysoft.com/doorway" Status="Successful" Token="87348a9665ffc984abf37329"> 
     <ns2:Availabilities> 
      <ns2:Availability CurrencyCode="USD" HotelCode="HY-CHIRC"> 
       <ns2:GuestCount AdultCount="1" ChildCount="0"/> 
       <ns2:RoomTypes> 
        <ns2:RoomType RoomTypeCode="KING" RoomTypeName="1 King Bed"> 
         <ns2:RoomTypeDescription>1 King Bed; 1 Grand Bed:Ihome Station:Large Plasma Tv:; Large Workdesk:Comp Gym</ns2:RoomTypeDescription> 
        </ns2:RoomType> 
        <ns2:RoomType RoomTypeCode="DLXK" RoomTypeName="High Floor King"> 
         <ns2:RoomTypeDescription>High Floor King; Guaranteed High Floor-25th and Up:350 Sq Ft; 1 King Grand Bed:Oversized Workdesk:Free Wifi</ns2:RoomTypeDescription> 
        </ns2:RoomType> 
        <ns2:RoomType RoomTypeCode="VW1K" RoomTypeName="Premium View King"> 
         <ns2:RoomTypeDescription>Premium View King; 1 King Bed:Ihome Station:Large Plasma Tv:; Large Workdesk:Comp Gym</ns2:RoomTypeDescription> 
        </ns2:RoomType> 
        <ns2:RoomType RoomTypeCode="CLBK" RoomTypeName="Regency Club King"> 
         <ns2:RoomTypeDescription>Regency Club King; Comp Contl Breakfast:Hors D Oeuvres and Dessert:; Exclusive Club Lounge With View</ns2:RoomTypeDescription> 
        </ns2:RoomType> 
        <ns2:RoomType RoomTypeCode="QNQN" RoomTypeName="2 Queen Beds"> 
         <ns2:RoomTypeDescription>2 Queen Beds; 2 Queen Grand Beds:Ihome Station:Plasma Tv:; Large Workdesk:Comp Gym</ns2:RoomTypeDescription> 
        </ns2:RoomType> 
        <ns2:RoomType RoomTypeCode="VW2Q" RoomTypeName="Premium View 2 Qen"> 
         <ns2:RoomTypeDescription>Premium View 2 Qen; 2 Queen Beds:Ihome Station:Large Plasma Tv:; Large Workdesk:Comp Gym</ns2:RoomTypeDescription> 
        </ns2:RoomType> 
        <ns2:RoomType RoomTypeCode="JRSQ" RoomTypeName="Grand 2 Queens"> 
         <ns2:RoomTypeDescription>Grand 2 Queens; Oversized Corner Room:2 Queen Beds:420 Sq Ft:; 2 Soft Chairs:Sml Table:Large Desk:Free Wifi</ns2:RoomTypeDescription> 
        </ns2:RoomType> 
        <ns2:RoomType RoomTypeCode="CLBD" RoomTypeName="Regency Club 2 Double Beds Bd"> 
         <ns2:RoomTypeDescription>Regency Club 2 Double Beds Bd; Comp Contl Breakfast:Hors D Oeuvres and Dessert:; Exclusive Club Lounge With View</ns2:RoomTypeDescription> 
        </ns2:RoomType> 
        <ns2:RoomType RoomTypeCode="JRSK" RoomTypeName="Junior City View Suite"> 
         <ns2:RoomTypeDescription>Junior City View Suite; Large Parlor With 1 King Bed Grand Bed:Sitting Area:; Oversize Couch:Large Desk:Plasma Tv</ns2:RoomTypeDescription> 
        </ns2:RoomType> 
        <ns2:RoomType RoomTypeCode="DLXD" RoomTypeName="High Fl 2 Double"> 
         <ns2:RoomTypeDescription>High Fl 2 Double; Guaranteed High Floor-25th and Up:350 Sq Ft; 2 Double Beds:Oversized Workdesk:Free Wifi</ns2:RoomTypeDescription> 
        </ns2:RoomType> 
       </ns2:RoomTypes> 
       <ns2:RatePlans> 
        <ns2:RatePlan RatePlanCode="49584IPRTF" RatePlanName="Partner Rate"> 
         <ns2:RatePlanDescription/> 
         <ns2:CancelPolicy NonRefundable="false"> 
          <ns2:Description>CXL 24HRS PRIOR TO ARRIVAL TO AVOID 1NT PNLTY</ns2:Description> 
          <ns2:CancelPenalties> 
           <ns2:CancelPenalty MaxHoursBeforeCheckIn="24" MinHoursBeforeCheckIn="0" Nights="1" NoShow="false"/> 
           <ns2:CancelPenalty Nights="1" NoShow="true"/> 
          </ns2:CancelPenalties> 
         </ns2:CancelPolicy> 
        </ns2:RatePlan> 
        <ns2:RatePlan RatePlanCode="49584WPAWAF" RatePlanName="Bed And Breakfast"> 
         <ns2:RatePlanDescription/> 
         <ns2:CancelPolicy NonRefundable="false"> 
          <ns2:Description>CXL 24HRS PRIOR TO ARRIVAL TO AVOID 1NT PNLTY</ns2:Description> 
          <ns2:CancelPenalties> 
           <ns2:CancelPenalty MaxHoursBeforeCheckIn="24" MinHoursBeforeCheckIn="0" Nights="1" NoShow="false"/> 
           <ns2:CancelPenalty Nights="1" NoShow="true"/> 
          </ns2:CancelPenalties> 
         </ns2:CancelPolicy> 
        </ns2:RatePlan> 
       </ns2:RatePlans> 
       <ns2:RoomRates> 
        <ns2:RoomRate RatePlanCode="49584IPRTF" RoomTypeCode="KING"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="178.610" AmountBeforeTax="152.150" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584IPRTF" RoomTypeCode="DLXK"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="198.570" AmountBeforeTax="169.150" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584IPRTF" RoomTypeCode="VW1K"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="218.520" AmountBeforeTax="186.150" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584IPRTF" RoomTypeCode="CLBK"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="233.490" AmountBeforeTax="198.900" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584IPRTF" RoomTypeCode="QNQN"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="178.610" AmountBeforeTax="152.150" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584IPRTF" RoomTypeCode="VW2Q"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="218.520" AmountBeforeTax="186.150" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584IPRTF" RoomTypeCode="JRSQ"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="253.450" AmountBeforeTax="215.900" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584IPRTF" RoomTypeCode="CLBD"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="233.490" AmountBeforeTax="198.900" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584WPAWAF" RoomTypeCode="KING"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="208.540" AmountBeforeTax="177.650" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584WPAWAF" RoomTypeCode="DLXK"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="218.520" AmountBeforeTax="186.150" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584WPAWAF" RoomTypeCode="VW1K"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="238.480" AmountBeforeTax="203.150" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584WPAWAF" RoomTypeCode="JRSK"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="448.020" AmountBeforeTax="381.650" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584WPAWAF" RoomTypeCode="QNQN"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="198.570" AmountBeforeTax="169.150" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584WPAWAF" RoomTypeCode="DLXD"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="218.520" AmountBeforeTax="186.150" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584WPAWAF" RoomTypeCode="VW2Q"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="238.480" AmountBeforeTax="203.150" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
        <ns2:RoomRate RatePlanCode="49584WPAWAF" RoomTypeCode="JRSQ"> 
         <ns2:Rates> 
          <ns2:Rate AmountAfterTax="448.020" AmountBeforeTax="381.650" EffectiveDate="2016-05-13" ExpireDate="2016-05-14"/> 
         </ns2:Rates> 
        </ns2:RoomRate> 
       </ns2:RoomRates> 
      </ns2:Availability> 
     </ns2:Availabilities> 
    </ns2:AvailabilityResponse> 
</SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

、私は唯一の特定のRatePlanCodeに関連CancelPolicyをしたいです。たとえば、RatePlanCode "49584WPAWAF"のCancelPolicyが必要な場合、このように返す必要があります。この戻りXMLは文字列の形式にする必要があります。

<ns2:CancelPolicy NonRefundable="false"> 
          <ns2:Description>CXL 24HRS PRIOR TO ARRIVAL TO AVOID 1NT PNLTY</ns2:Description> 
          <ns2:CancelPenalties> 
           <ns2:CancelPenalty MaxHoursBeforeCheckIn="24" MinHoursBeforeCheckIn="0" Nights="1" NoShow="false"/> 
           <ns2:CancelPenalty Nights="1" NoShow="true"/> 
          </ns2:CancelPenalties> 
         </ns2:CancelPolicy> 

どうすればこの問題に近づくことができますか。私が使うことができる図書館は言及しないでください。そのライブラリを使用してソリューションを提供してください。おかげであなたはRatePlanCode属性値に基づいてRatePlanを見つけるために、次のXPathを使用して、子要素CancelPolicyを返すことができ

答えて

2

//ns2:RatePlan[@RatePlanCode='49584WPAWAF']/ns2:CancelPolicy 

あなたはxml.etreeを使用してXPathを実行、または次のように* lxmlを使用することができます

from lxml import etree 

raw = '''your xml string here''' 
root = etree.fromstring(raw) 
ns = {'ns2': 'http://www.derbysoft.com/doorway'} 
query = "//ns2:RatePlan[@RatePlanCode='49584WPAWAF']/ns2:CancelPolicy" 
result = root.xpath(query, namespaces=ns) 
print etree.tostring(result[0]) 
+0

私はあなたのコードを試しましたが、このエラーが発生しました:IndexError:リストのインデックスが範囲外です.. –

+0

ありがとう!それは働いた:) –

+0

私は名前空間が欲しくない:(xmlns:ns2 = "http://www.derbysoft.com/doorway" xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/封筒/)を私の結果で削除するにはどうすればいいですか? –

関連する問題