Elementtree, check if element has certain parent?
Question: I am parsing an xml file: <http://pastebin.com/fw151jQN> I wish to read it in
copy a lot of it and write it to a new file, some of it modified, a lot of it
unmodified, and a lot of it ignored. As an initial pass I want to find certain
xml, and write it to a new file unchanged.

Here is the section of the xml that is of interest at first:

    
    
    <COMMAND name="shutdown"
            help="Shutdown the selected interface">
            <CONFIG priority="0x7F01" />
            <ACTION>
            /klas/klish-scripts/interfaces.py conf -i ${iface} --enable 0
            </ACTION>
        </COMMAND>
    
        <COMMAND name="no shutdown"
            help="Enable the selected interface">
            <CONFIG operation="unset" pattern="shutdown"/>
            <ACTION>
            /klas/klish-scripts/interfaces.py conf -i ${iface} --enable 1
            </ACTION>
        </COMMAND>
    

My code is below

    
    
    #!/usr/bin/python -tt
    
    import xml.etree.ElementTree as ET
    tree = ET.parse('interface_range_test.xml')
    root = tree.getroot()
    namespaces = {'command': 'http://clish.sourceforge.net/XMLSchema}COMMAND','config': 'http://clish.sourceforge.net/XMLSchema}CONFIG'}
    
    all_links = tree.findall('.//')
    
    for i in all_links: 
        if namespaces['command'] in i.tag:
            if i.attrib['name'] == "shutdown":
                print i.attrib       
        if namespaces['config'] in i.tag: 
            print i.attrib
    

Output:

    
    
    {'name': 'shutdown', 'help': 'Shutdown the selected interface'}
    {'priority': '0x7F01'}
    {'pattern': 'shutdown', 'operation': 'unset'}
    

This reads in the file and I can find the shutdown information, now I want to
find the `CONFIG` information, and then the `action` information and it's
text, but when I search there is `CONFIG` information for both `shutdown` and
`no shutdown`. This case will occur in a lot of the xml, a lot of it has the
same format.

Shutdown: {'priority': '0x7F01'} no shutdown: {'pattern': 'shutdown',
'operation': 'unset'}

How can I specify which to look at, can I check the parent of this
information? Or can I check the children of the super element above it
(`http://clish.sourceforge.net/XMLSchema}COMMAND`)?


Answer: You can search all COMMANDS as node（element), and getting CONFIG info from
there, e.g.

    
    
    import xml.etree.ElementTree as ET
    tree = ET.parse('interface_range_test.xml')
    root = tree.getroot()
    
    for command in root.iter("{http://clish.sourceforge.net/XMLSchema}COMMAND"):
        subs = list(command.iter('{http://clish.sourceforge.net/XMLSchema}CONFIG'))
        if len(subs） > 0: #we found CONFIG
            print command.tag, command.attrib, subs[0].tag, subs[0].attrib
    

And you will get:

    
    
    {http://clish.sourceforge.net/XMLSchema}COMMAND {'name': 'shutdown', 'help': 'Shutdown the selected interface'} {http://clish.sourceforge.net/XMLSchema}CONFIG {'priority': '0x7F01'}
    {http://clish.sourceforge.net/XMLSchema}COMMAND {'name': 'no shutdown', 'help': 'Enable the selected interface'} {http://clish.sourceforge.net/XMLSchema}CONFIG {'pattern': 'shutdown', 'operation': 'unset'}
    

BTW, if you need to handling big xml files, I would recommend to use
[lxml](http://lxml.de), which also has [ElementTree compatible
interface](http://lxml.de/tutorial.html) but much faster than python's
standard xml lib.

