1

I'm using the documentation here to try to get only the values (address , mask ) for certain elements.

This is an example of the structure of my XML:

    <?xml version="1.0" ?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:52622325-b136-40cf-bc36-85332e25b6f3" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
    <data>
            <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
                    <interface>
                            <GigabitEthernet>
                                    <name>1</name>
                                    <ip>
                                            <address>
                                                    <primary>
                                                            <address>192.168.40.30</address>
                                                            <mask>255.255.255.0</mask>
                                                    </primary>
                                            </address>
                                    </ip>
                                    <logging>
                                            <event>
                                                    <link-status/>
                                                    </event>
                                            </logging>
                                            <mop>
                                                    <enabled>false</enabled>
                                                    <sysid>false</sysid>
                                            </mop>
                                            <negotiation xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-ethernet">
                                                    <auto>true</auto>
                                            </negotiation>
                                    </GigabitEthernet>
                                    <GigabitEthernet>
                                            <name>2</name>
                                            <ip>
                                                    <address>
                                                            <primary>
                                                                    <address>10.10.10.1</address>
                                                                    <mask>255.255.255.0</mask>
                                                            </primary>
                                                    </address>
                                            </ip>
                                            <logging>
                                                    <event>
                                                            <link-status/>
                                                    </event>
                                            </logging>
                                            <mop>
                                                    <enabled>false</enabled>
                                                    <sysid>false</sysid>
                                            </mop>
                                            <negotiation xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-ethernet">
                                                    <auto>true</auto>
                                            </negotiation>
                                    </GigabitEthernet>
                                    <GigabitEthernet>
                                            <name>3</name>
                                            <ip>
                                                    <address>
                                                            <primary>
                                                                    <address>30.30.30.1</address>
                                                                    <mask>255.255.255.0</mask>
                                                            </primary>
                                                    </address>
                                            </ip>
                                            <logging>
                                                    <event>
                                                            <link-status/>
                                                    </event>
                                            </logging>
                                            <mop>
                                                    <enabled>false</enabled>
                                                    <sysid>false</sysid>
                                            </mop>
                                            <negotiation xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-ethernet">
                                                    <auto>true</auto>
                                            </negotiation>
                                    </GigabitEthernet>
                                    <GigabitEthernet>
                                            <name>4</name>
                                            <logging>
                                                    <event>
                                                            <link-status/>
                                                    </event>
                                            </logging>
                                            <mop>
                                                    <enabled>false</enabled>
                                                    <sysid>false</sysid>
                                            </mop>
                                            <negotiation xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-ethernet">
                                                    <auto>true</auto>
                                            </negotiation>
                                    </GigabitEthernet>
                            </interface>
                    </native>
            </data>

Working off this example in the documentation, I've tried something like this:

import xml.etree.ElementTree as ET
tree = ET.parse("C:\\Users\\Redha\\Documents\\test_network\\interface123.xml")
root = tree.getroot()

for i in root.findall('native'):  
  print(i.tag)

But it returns nothing . I've tried other things to no success. Any ideas? All advice appreciated. Thank you!

8
  • Have you tried the examples in the documentation with the XML also in the documentation? docs.python.org/3/library/… Commented May 24, 2021 at 14:55
  • @barny yes i did Commented May 24, 2021 at 15:06
  • 1
    Check the bit about namespaces - native has a namespace. Commented May 24, 2021 at 15:22
  • @barny i did but its dosen't enter in the loop Commented May 24, 2021 at 15:23
  • 1
    Please read docs.python.org/3/library/… Commented May 24, 2021 at 15:42

1 Answer 1

1

Consider using namespaces when referencing XML elements:

import xml.etree.ElementTree as ET

# declare XML namespaces
namespaces = {'native': 'http://cisco.com/ns/yang/Cisco-IOS-XE-native'}

tree = ET.parse("C:\\Users\\Redha\\Documents\\test_network\\interface123.xml")
root = tree.getroot()

# call findall() using previously created namespaces map
for i in root.findall('.//native:native', namespaces):  
  print(i.tag)
Sign up to request clarification or add additional context in comments.

3 Comments

i forget ( <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:52622325-b136-40cf-bc36-85332e25b6f3" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"> ) under ( <?xml version="1.0" encoding="UTF-8"?> ) that this effect ?
Updated answer to user relative path from the root.
thank you so much i really appriciate your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.