xmltodictライブラリを使用してSOAP応答を解析しています。ここでSOAP xmlの応答から詳細を取得します
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns2:MultiAvailabilityResponse xmlns:ns2="http://www.derbysoft.com/doorway" Status="Successful" Token="187be58c62c2f2515b5d78ee">
<ns2:Availabilities>
<ns2:Availability CurrencyCode="GBP" HotelCode="HY-LONGE">
<ns2:GuestCount AdultCount="1" ChildCount="0"/>
<ns2:RoomTypes>
<ns2:RoomType RoomTypeCode="KING" RoomTypeName="Andaz King">
<ns2:RoomTypeDescription>A 29-square-metre room ,Modern furnishings include oversized work desk, plus bathroom with fast-fill tub and heated towel rail.</ns2:RoomTypeDescription>
</ns2:RoomType>
<ns2:RoomType RoomTypeCode="TWIN" RoomTypeName="Andaz Twin">
<ns2:RoomTypeDescription>A 29-square-metre room ,Modern furnishings include oversized work desk, plus bathroom with fast-fill tub and heated towel rail.</ns2:RoomTypeDescription>
</ns2:RoomType>
<ns2:RoomType RoomTypeCode="QUEN" RoomTypeName="Andaz Queen">
<ns2:RoomTypeDescription>A 26-square-metre room ,Modern furnishings include oversized work desk, plus bathroom with fast-fill tub and heated towel rail.</ns2:RoomTypeDescription>
</ns2:RoomType>
</ns2:RoomTypes>
<ns2:RatePlans>
<ns2:RatePlan RatePlanCode="49584WADPF2" RatePlanName="Advance Purchase">
<ns2:RatePlanDescription>Advance Purchase</ns2:RatePlanDescription>
</ns2:RatePlan>
<ns2:RatePlan RatePlanCode="49584WADPF" RatePlanName="Advance Purchase">
<ns2:RatePlanDescription>Advance Purchase</ns2:RatePlanDescription>
</ns2:RatePlan>
<ns2:RatePlan RatePlanCode="49584IPRTF" RatePlanName="Partner Rate">
<ns2:RatePlanDescription>Partner Rate</ns2:RatePlanDescription>
</ns2:RatePlan>
</ns2:RatePlans>
<ns2:RoomRates>
<ns2:RoomRate RatePlanCode="49584WADPF2" RoomTypeCode="KING">
<ns2:Rates>
<ns2:Rate AmountAfterTax="249.900" AmountBeforeTax="249.900" EffectiveDate="2016-05-05" ExpireDate="2016-05-06"/>
</ns2:Rates>
<ns2:Fees>
<ns2:Fee Amount="0.000" ChargeType="Tax" Type="Exclusive" Unit="PER_ROOM_PER_NIGHT"/>
</ns2:Fees>
</ns2:RoomRate>
<ns2:RoomRate RatePlanCode="49584WADPF2" RoomTypeCode="TWIN">
<ns2:Rates>
<ns2:Rate AmountAfterTax="249.900" AmountBeforeTax="249.900" EffectiveDate="2016-05-05" ExpireDate="2016-05-06"/>
</ns2:Rates>
<ns2:Fees>
<ns2:Fee Amount="0.000" ChargeType="Tax" Type="Exclusive" Unit="PER_ROOM_PER_NIGHT"/>
</ns2:Fees>
</ns2:RoomRate>
<ns2:RoomRate RatePlanCode="49584WADPF" RoomTypeCode="QUEN">
<ns2:Rates>
<ns2:Rate AmountAfterTax="249.900" AmountBeforeTax="249.900" EffectiveDate="2016-05-05" ExpireDate="2016-05-06"/>
</ns2:Rates>
<ns2:Fees>
<ns2:Fee Amount="0.000" ChargeType="Tax" Type="Exclusive" Unit="PER_ROOM_PER_NIGHT"/>
</ns2:Fees>
</ns2:RoomRate>
</ns2:RoomRates>
</ns2:Availability>
<ns2:Availability CurrencyCode="USD" HotelCode="HY-CHIRC">
<ns2:GuestCount AdultCount="1" ChildCount="0"/>
<ns2:RoomTypes>
<ns2:RoomType RoomTypeCode="JRSQ" RoomTypeName="JR SUITE 2 QUEEN BEDS">
<ns2:RoomTypeDescription>Rest in sublime comfort on one of two queen signature Hyatt Grand Beds®, fitted with fine linens, down blanket and plump pillows.</ns2:RoomTypeDescription>
</ns2:RoomType>
<ns2:RoomType RoomTypeCode="CLBD" RoomTypeName="REG CLUB 2 DOUBLE BEDS">
<ns2:RoomTypeDescription>one King or two double-sized Hyatt Grand Beds, fitted with luxurious linens, a down blanket and plush pillows</ns2:RoomTypeDescription>
</ns2:RoomType>
</ns2:RoomTypes>
<ns2:RatePlans>
<ns2:RatePlan RatePlanCode="49584IPRTF" RatePlanName="Partner Rate">
<ns2:RatePlanDescription>Partner Rate</ns2:RatePlanDescription>
<ns2:CancelPolicy NonRefundable="true">
<ns2:CancelPenalties/>
</ns2:CancelPolicy>
</ns2:RatePlan>
<ns2:RatePlan RatePlanCode="49584WPAWAF" RatePlanName="Bed and Breakfast">
<ns2:RatePlanDescription>Bed and Breakfast</ns2:RatePlanDescription>
<ns2:CancelPolicy NonRefundable="true">
<ns2:CancelPenalties/>
</ns2:CancelPolicy>
</ns2:RatePlan>
</ns2:RatePlans>
<ns2:RoomRates>
<ns2:RoomRate RatePlanCode="49584IPRTF" RoomTypeCode="JRSQ">
<ns2:Rates>
<ns2:Rate AmountAfterTax="543.134" AmountBeforeTax="466.650" EffectiveDate="2016-05-05" ExpireDate="2016-05-06"/>
</ns2:Rates>
<ns2:Fees>
<ns2:Fee ChargeType="Tax" Percent="16.390" Type="Exclusive"/>
</ns2:Fees>
</ns2:RoomRate>
<ns2:RoomRate RatePlanCode="49584IPRTF" RoomTypeCode="CLBD">
<ns2:Rates>
<ns2:Rate AmountAfterTax="370.004" AmountBeforeTax="317.900" EffectiveDate="2016-05-05" ExpireDate="2016-05-06"/>
</ns2:Rates>
<ns2:Fees>
<ns2:Fee ChargeType="Tax" Percent="16.390" Type="Exclusive"/>
</ns2:Fees>
</ns2:RoomRate>
<ns2:RoomRate RatePlanCode="49584IPRTF" RoomTypeCode="VW2Q">
<ns2:Rates>
<ns2:Rate AmountAfterTax="325.485" AmountBeforeTax="279.650" EffectiveDate="2016-05-05" ExpireDate="2016-05-06"/>
</ns2:Rates>
<ns2:Fees>
<ns2:Fee ChargeType="Tax" Percent="16.390" Type="Exclusive"/>
</ns2:Fees>
</ns2:RoomRate>
</ns2:RoomRates>
</ns2:Availability>
</ns2:Availabilities>
</ns2:MultiAvailabilityResponse>
</SOAP-ENV:Body>
SOAP応答から必要な詳細情報を取得するための私のコードです:
def listify(obj):
"""To convert each element of SOAP into a list, so processing of response would be easy."""
if isinstance(obj, list):
return obj
return [obj]
def search_hotels_formatted_response(soap):
"""Parse the response."""
soap = xmltodict.parse(soap, process_namespaces=True)
# Deal with namespaces
env = 'http://schemas.xmlsoap.org/soap/envelope/:'
doorway = 'http://www.derbysoft.com/doorway:'
availability = listify(
soap[env + 'Envelope']
[env + 'Body']
[doorway + 'MultiAvailabilityResponse']
[doorway + 'Availabilities']
[doorway + 'Availability'])
# Intermediate data structure to hold room names
names = {
roomtype['@RoomTypeCode']: roomtype['@RoomTypeName']
for _availability in availability
for roomtype in listify(_availability
[doorway + 'RoomTypes']
[doorway + 'RoomType'])
}
return_dict = {
'ibp': 'dbs',
'rL': sorted([
{
'rtc': roomrate['@RoomTypeCode'],
'rpc': roomrate['@RatePlanCode'],
'rtn': names[roomrate['@RoomTypeCode']],
'rmt': rate['@AmountBeforeTax'],
'cur': _availability['@CurrencyCode'],
'ttc': float(rate['@AmountAfterTax']) - float(rate['@AmountBeforeTax']),
'egc': 0,
'long': ''
}
for _availability in availability
for roomrate in listify(_availability
[doorway + 'RoomRates']
[doorway + 'RoomRate'])
for rate in listify(roomrate
[doorway + 'Rates']
[doorway + 'Rate'])],
key=lambda x: float(x['rmt'])),
'hc': _availability['@HotelCode'],
'hn': ''
}
return return_dict
私は辞書の必要な形式にSOAPメッセージから必要な詳細情報を取得することができています。しかし、私は上で共有されたこの特定のSOAPメッセージから詳細を取得する際に問題を抱えています。他のすべてのケースでは、私はちょうどそれの組み合わせの異なる数を持つ単一のホテルの応答を得ていたが、このSOAP応答で私は2つのホテルがあり、私の解析コードは、両方のホテルの部屋の価格を同時に取得しているそれをソートします。しかし、私はセコングホテルのオプションで最初のホテルのオプションでソートをしたい。ホテルの数は様々である可能性があるので、ホテルの数に関係なく機能する一般的なソリューションを提供してください。
すべてのヘルプが評価されます。ありがとう!
は、あなたもその中に助けることができます:) –
所望の出力、リストの具体的な構造は何ですか? JSON形式のようですか? – Parfait