Home > Net >  Parsing XML with Namespaces - Elementtree
Parsing XML with Namespaces - Elementtree

Time:01-31

I need assistance with parsing xml that contains namespaces. I am using ElementTree and having no luck. Below is the xml file, which I’m trying to pull out the “Status”, “Status Reason”, and “IP”. I’ve been searching for info regarding ElementTree and namespaces and running into a wall. 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>

CodePudding user response:

to find elements by Tagname in an xml file you can use 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)

CodePudding user response:

Assuming you are using Python version >=3.8, you can use namespace wildcards:.

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:

Registered
0
192.168.100.12
  • Related