Return all tags and values of a given object within xml document in Python
Question: I have xml which looks like this...

    
    
    <CONFIG2>
        <OBJECT id="{2D3474AA-9A0F-4696-979C-1BCE9350F3BD}" type="3" name="Test2" rev="1">
            <RULEITEM>
                <ID>{BF7D00C5-57BC-4187-9B07-064BA5744A12}</ID>
                <PROPERTIES>
                    <columnname/>
                    <columnvalue/>
                    <days>|</days>
                    <enabled>1</enabled>
                    <eventdate>0001-01-01</eventdate>
                    <eventtime>00:00:00</eventtime>
                    <function>average</function>
                    <parameters/>
                    <stattype>standard</stattype>
                </PROPERTIES>
                <ITEM>
                    <ID>{61C82F62-8F31-4754-A705-7CCBB34C6FD4}</ID>
                    <PROPERTIES>
                        <actionindex>0</actionindex>
                        <actiontype>eventlog</actiontype>
                        <severity>error</severity>
                    </PROPERTIES>
                </ITEM>
            </RULEITEM>
            <PROPERTIES>
                <groups>|</groups>
            </PROPERTIES>
        </OBJECT>
    

I am trying to return everything within OBJECT and /OBJECT.

For example, I would like to return all tags and values for the OBJECT where
type="3" and name="test2".

Here is my current python script...

    
    
    import xml.etree.ElementTree as ET
    tree = ET.parse(r'C:\Users\User\Desktop\xml\config.xml')
    root = tree.getroot()
    
    objectType = input("What object type are you looking for?: ")
    
    for item in root.findall('OBJECT'):
        if item.attrib['type'] == objectType:
            print(item.get('name'))
    
    objectName = input("What is the name of the object you are looking for?: ")
    
    for item in root.findall('OBJECT'):
        if item.attrib['type'] == objectType and item.get('name') == objectName:
            print(list(item))
    

This returns...

    
    
     <Element "RULEITEM' at 0x00064F3C0>, <Element 'PROPERTIES' at 0x0064FB40>
    

I like it to return the entire object and all tags and values. Does anyone
know how I could do this?

Thanks!


Answer: Consider adjusting second loop by using another `findall()` iteration on all
of `OBJECT's` children and grandchildren and so forth using (`//*`). Also,
consider appending to a pre-defined list instead of `list()` (which would have
broken each character of the loops' text item) and condition it to remove
`None` values (i.e., empty nodes). Finally, use the `tag` and `text` property
of the node:

    
    
    ... same code as above...
    
    values = []
    for item in root.findall('OBJECT'):
        if item.attrib['type'] == objectType and item.get('name') == objectName:
            for data in root.findall("OBJECT//*"):
                if data.text is not None:
                    values.append(data.tag + ': ' + data.text)
    
    for v in values:
        print(v)
    
    #What object type are you looking for?: 3
    #Test2
    #What is the name of the object you are looking for?: Test2
    #RULEITEM: 
    #            
    #ID: {BF7D00C5-57BC-4187-9B07-064BA5744A12}
    #PROPERTIES: 
    #                
    #days: |
    #enabled: 1
    #eventdate: 0001-01-01
    #eventtime: 00:00:00
    #function: average
    #stattype: standard
    #ITEM: 
    #                
    #ID: {61C82F62-8F31-4754-A705-7CCBB34C6FD4}
    #PROPERTIES: 
    #                    
    #actionindex: 0
    #actiontype: eventlog
    #severity: error
    #PROPERTIES: 
    #            
    #groups: |
    

