如何在python中解析SOAP xml?

2024-06-25 06:55:16 发布

您现在位置:Python中文网/ 问答频道 /正文

<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解析它


Tags: orgcomhttpwwwxmlairenvelopexmlns
1条回答
网友
1楼 · 发布于 2024-06-25 06:55:16

试试这个

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\U 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'])

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

相关问题 更多 >