如何在python中解析SOAP xml? 达伦 史蒂文斯 `将xml.etree.ElementTree作为ET导入

如何在python中解析SOAP xml? 达伦 史蒂文斯 `将xml.etree.ElementTree作为ET导入,python,xml,soap,Python,Xml,Soap,mytree=ET.parse('check_in.xml')) 它将给我解析的xml的根元素 myroot=mytree.getroot() 打印(myroot) 打印(myroot.tag) ` 这是一个SOAP XML,我想用python解析它。试试这个 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:chec="http://www.ed

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

它将给我解析的xml的根元素 myroot=mytree.getroot() 打印(myroot) 打印(myroot.tag) `

这是一个SOAP XML,我想用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
结果:

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'}
循环EDS\U AirCheckInRQ

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'))

还有更多的例子。此库易于使用。

要从xml中提取哪些信息?请使用beautifulsoup进行同样的操作。您可以找到示例:这是SOAP XML,所以我觉得很难?如果你有什么想法?谢谢,上面的代码对我来说很好。如果有人知道smiplifiedDoc有什么用?@MinhajKhan它是一个用于解析XML和HTML等数据的库。simplified_scrapy它是一个用于提取数据的框架,对吗?我可以选择多个节点并打印数据吗?@MinhajKhan我更新了答案。也许它能帮助你。
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'])