简体   繁体   English

使用命名空间解析 XML - Elementtree

[英]Parsing XML with Namespaces - Elementtree

I need assistance with parsing xml that contains namespaces.我需要帮助来解析包含命名空间的 xml。 I am using ElementTree and having no luck.我正在使用 ElementTree 并且没有运气。 Below is the xml file, which I'm trying to pull out the “Status”, “Status Reason”, and “IP”.下面是 xml 文件,我试图从中提取“状态”、“状态原因”和“IP”。 I've been searching for info regarding ElementTree and namespaces and running into a wall.我一直在搜索有关 ElementTree 和名称空间的信息,但遇到了麻烦。 Please help.请帮忙。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns1:selectCmDeviceResponse xmlns:ns1="http://schemas.cisco.com/ast/soap">
         <ns1:selectCmDeviceReturn>
            <ns1:SelectCmDeviceResult>
               <ns1:TotalDevicesFound>1</ns1:TotalDevicesFound>
               <ns1:CmNodes>
                  <ns1:item>
                     <ns1:ReturnCode>Ok</ns1:ReturnCode>
                     <ns1:Name>cucm125c</ns1:Name>
                     <ns1:NoChange>false</ns1:NoChange>
                     <ns1:CmDevices>
                        <ns1:item>
                           <ns1:Name>SEPxxxxxxxxxxxxC</ns1:Name>
                           <ns1:DirNumber>8564212345-Registered</ns1:DirNumber>
                           <ns1:DeviceClass>Phone</ns1:DeviceClass>
                           <ns1:Model>36224</ns1:Model>
                           <ns1:Product>36677</ns1:Product>
                           <ns1:BoxProduct>0</ns1:BoxProduct>
                           <ns1:Httpd>Yes</ns1:Httpd>
                           <ns1:RegistrationAttempts>0</ns1:RegistrationAttempts>
                           <ns1:IsCtiControllable>true</ns1:IsCtiControllable>
                           <ns1:LoginUserId xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                           <ns1:Status>Registered</ns1:Status>
                           <ns1:StatusReason>0</ns1:StatusReason>
                           <ns1:PerfMonObject>2</ns1:PerfMonObject>
                           <ns1:DChannel>0</ns1:DChannel>
                           <ns1:Description>SEPxxxxxxxxxxxxC</ns1:Description>
                           <ns1:H323Trunk>
                              <ns1:ConfigName xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                              <ns1:TechPrefix xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                              <ns1:Zone xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                              <ns1:RemoteCmServer1 xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                              <ns1:RemoteCmServer2 xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                              <ns1:RemoteCmServer3 xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                              <ns1:AltGkList xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                              <ns1:ActiveGk xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                              <ns1:CallSignalAddr xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                              <ns1:RasAddr xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                           </ns1:H323Trunk>
                           <ns1:TimeStamp>1672938826</ns1:TimeStamp>
                           <ns1:Protocol>SIP</ns1:Protocol>
                           <ns1:NumOfLines>1</ns1:NumOfLines>
                           <ns1:LinesStatus>
                              <ns1:item>
                                 <ns1:DirectoryNumber>8564212345</ns1:DirectoryNumber>
                                 <ns1:Status>Registered</ns1:Status>
                              </ns1:item>
                           </ns1:LinesStatus>
                           <ns1:ActiveLoadID>sip8845_65.12-8-1-0001-455</ns1:ActiveLoadID>
                           <ns1:InactiveLoadID>sip8845_65.14-0-1-0001-135</ns1:InactiveLoadID>
                           <ns1:DownloadStatus>Unknown</ns1:DownloadStatus>
                           <ns1:DownloadFailureReason xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                           <ns1:DownloadServer xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                           <ns1:IPAddress>
                              <ns1:item>
                                 <ns1:IP>192.168.100.12</ns1:IP>
                                 <ns1:IPAddrType>ipv4</ns1:IPAddrType>
                                 <ns1:Attribute>Unknown</ns1:Attribute>
                              </ns1:item>
                           </ns1:IPAddress>
                        </ns1:item>
                     </ns1:CmDevices>
                  </ns1:item>
               </ns1:CmNodes>
            </ns1:SelectCmDeviceResult>
            <ns1:StateInfo>&lt;StateInfo ClusterWide="1">&lt;Node Name="cucm125c" SubsystemStartTime="1671301636" StateId="109" TotalItemsFound="1" TotalItemsReturned="1"/>&lt;/StateInfo></ns1:StateInfo>
         </ns1:selectCmDeviceReturn>
      </ns1:selectCmDeviceResponse>
   </soapenv:Body>
</soapenv:Envelope>

to find elements by Tagname in an xml file you can use minidom.要在 xml 文件中按标记名查找元素,您可以使用 minidom。 The code could look like this:代码可能如下所示:

from xml.dom.minidom import parse

dom1 = parse('gg.xml')

for subelement in dom1.getElementsByTagName("ns1:Status"):
    print(subelement.tagName, subelement.firstChild.data)

for subelement in dom1.getElementsByTagName("ns1:StatusReason"):
    print(subelement.tagName, subelement.firstChild.data)

for subelement in dom1.getElementsByTagName("ns1:IP"):
    print(subelement.tagName, subelement.firstChild.data)

Assuming you are using Python version >=3.8, you can use namespace wildcards: .假设您使用的是 Python 版本 >=3.8,您可以使用名称空间通配符:

In your case it should look something like:在您的情况下,它应该类似于:

items = your_parsed_soap.findall('.//{*}CmDevices')

for item in items:    
    print(item.find('.//{*}Status').text)
    print(item.find('.//{*}StatusReason').text)
    print(item.find('.//{*}IPAddress//{*}IP').text)

Output should be: Output 应该是:

Registered
0
192.168.100.12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM