如何在python中解析SOAP xml


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:chec="http://www.eds.com/AirlineSOASchema/CheckIn/" xmlns:air="http://www.eds.com/AirlineSOASchema/AirCheckInRQ" xmlns:com="http://www.opentravel.org/OTA/2003/05/CommonTypes" xmlns:com1="http://www.eds.com/AirlineSOASchema/AirCommonTypes" xmlns:air1="http://www.opentravel.org/OTA/2003/05/AirCommonTypes">
<soapenv:Header/>
<soapenv:Body>
<chec:checkIn>
<air:EDS_AirCheckInRQ Version="4.000">
<air:POS>
<com:Source AirlineVendorID="CM"/>
</air:POS>
<air:MessageFunction Function="CheckIn"/>
<air:FlightInfo RPH="1">
<com1:CarrierInfo Code="CM" FlightNumber="360"/>
<com1:DepartureInformation DateOfDeparture="2020-10-29T00:00:00" LocationCode="PTY"/>
</air:FlightInfo>
<air:PassengerInfo RPH="1" GivenNameRefNumber="1" SurnameRefNumber="1">
<com1:PassengerName>
<com:GivenName>DARREN</com:GivenName>
<com:Surname>STEVENS</com:Surname>
</com1:PassengerName>
<com1:PassengerType />
</air:PassengerInfo>
<air:PassengerFlightInfo PassengerRPH="1" FlightRPH="1">
<com1:SeatBoardingInfo SeatNumber="21C"/>
</air:PassengerFlightInfo>
<air:BaggageInfo PassengerRPH="1" CheckedBagCountTotal="0">
</air:BaggageInfo>
</air:EDS_AirCheckInRQ>
</chec:checkIn>
</soapenv:Body>
</soapenv:Envelope>`import xml.etree.ElementTree as ET

mytree=ET.parse('check_in.xml'(

它将给我解析xml的根元素

myroot=mytree.getroot((打印(myroot(打印(myroot.tag(`

这是一个SOAP XML,我想用python解析它。

试试这个。

from simplified_scrapy import SimplifiedDoc, utils
xml = utils.getFileContent('test.xml')
doc = SimplifiedDoc(xml)
nodes = doc.select('air:EDS_AirCheckInRQ').children
print (nodes.tag)
print (doc.select('air:EDS_AirCheckInRQ')['Version'])
print (doc.select('com:Source'))

结果:

['air:POS', 'air:MessageFunction', 'air:FlightInfo', 'air:PassengerInfo', 'air:PassengerFlightInfo', 'air:BaggageInfo']
4.000
{'tag': 'com:Source', 'AirlineVendorID': 'CM'}

把所有的标签都放在循环中。

from simplified_scrapy import SimplifiedDoc, utils
def loop(node):
print ([(k,v) for k,v in node.items() if k!='html']) # edited
children = node.children
if children:
for c in children:
loop(c)
else:
print ('tag, value: ', node.tag,',', node.text)
doc = SimplifiedDoc(utils.getFileContent('test.xml'))
loop(doc.select('soapenv:Envelope'))

环路EDS_AirCheckInRQ

from simplified_scrapy import SimplifiedDoc, utils
doc = SimplifiedDoc(utils.getFileContent('test.xml'))
lstEDS_AirCheckInRQ = doc.selects('air:EDS_AirCheckInRQ')
for EDS_AirCheckInRQ in lstEDS_AirCheckInRQ:
print (EDS_AirCheckInRQ['Version'])
print (EDS_AirCheckInRQ.select('com:Source')['AirlineVendorID'])
print (EDS_AirCheckInRQ.select('air:MessageFunction')['Function'])

下面是更多的例子。这个库很容易使用。

最新更新