Home > Software design >  Get all element with same name from xml using python
Get all element with same name from xml using python

Time:04-06

I try to get all the "username" element from config.xml using xml.etree.ElementTree. But cannot get the data using below code, it return None to me.

import xml.etree.ElementTree as ET
tree = ET.parse('config.xml')
root = tree.getroot()
users = tree.findall('username')
print(users)

If I use above code to test again some normal xml file, it works. Here is the config.xml file that I have to use, maybe there is format issue within this file, but I cannot figure out.

<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply
    xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:cf3e02a2-b7bc-475f-a87e-b6571eca99b7"
    xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
    <data>
        <app-hosting-cfg-data
            xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-app-hosting-cfg">
            <apps>
                <app>
                    <application-name>guestshell</application-name>
                    <application-network-resource>
                        <management-interface-name>0</management-interface-name>
                    </application-network-resource>
                </app>
            </apps>
        </app-hosting-cfg-data>
        <native
            xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
            <version>17.3</version>
            <boot-start-marker/>
            <boot-end-marker/>
            <enable>
                <secret>
                    <type>9</type>
                    <secret>$9$GNcSrWbM1PyCqU$9.BCza34ClqbgyABGzRV1v5hjCWhaoN9K.gqxCtcCvE</secret>
                </secret>
            </enable>
            <username>
                <name>developer</name>
                <privilege>15</privilege>
                <secret>
                    <encryption>9</encryption>
                    <secret>$9$oNguEA9um9vRx.$MsDk0DOy1rzBjKAcySWdNjoKcA7GetG9YNnKOs8S67A</secret>
                </secret>
            </username>
            <username>
                <name>netconf</name>
                <privilege>15</privilege>
                <secret>
                    <encryption>9</encryption>
                    <secret>$9$A7TadNm7VSWsXk$k0eVomchjOb90cjpI5hmLA/n0xlomzUhJ47o5y9WMHc</secret>
                </secret>
            </username>
            <username>
                <name>restconf</name>
                <privilege>15</privilege>
                <secret>
                    <encryption>9</encryption>
                    <secret>$14$eGCL$P15Lf8exJZB3qk$XYVi2eHOhT895Rpx1gYEQcwWfeML3AavoI/lxrMMtD.</secret>
                </secret>
            </username>
            <username>
                <name>root</name>
                <privilege>15</privilege>
                <secret>
                    <encryption>9</encryption>
                    <secret>$9$IRHgr7MCAEFNLk$ZfOrXoRLFhh6gHIxhLPfOC9KzjIWISv4KqkNAR51kmI</secret>
                </secret>
            </username>
        </native>
    </data>
</rpc-reply>

CodePudding user response:

Possible duplicate of this one.

Building on it you could do:

namespaces = {
    'Cisco-IOS-XE-native': "http://cisco.com/ns/yang/Cisco-IOS-XE-native"
}
for node in tree.findall(".//Cisco-IOS-XE-native:username", namespaces):
    print(node)

CodePudding user response:

How annoying are namespaces, right?

This simple XPath expression selects all tags named username in any (or no) namespace from anywhere in the entire tree.

users = tree.findall('.//{*}username')

Output:

[<Element '{http://cisco.com/ns/yang/Cisco-IOS-XE-native}username' at 0x000002352F7529D0>, <Element '{http://cisco.com/ns/yang/Cisco-IOS-XE-native}username' at 0x000002352F751620>, <Element '{http://cisco.com/ns/yang/Cisco-IOS-XE-native}username' at 0x000002352F750CC0>, <Element '{http://cisco.com/ns/yang/Cisco-IOS-XE-native}username' at 0x000002352F751FD0>]

It's worth noting that tree.findall('username') only finds tags named username which are the children of tree. It does not search recursively (i.e. the entire tree). In this case, tree only has one child, data.

  • Related