Filter xml data in Python
Question: Please help, Python beginner,

after getting all the data from xml, **data_list =
xmlTree.findall('.//data')** e.g here I get 10 rows Now, I need to keep only a
few rows for which attribute 'name' values match with elements of another list
(inputID) with three IDs inside. e.g. remains only 3 rows whose name attribute
match with the list elements

Thank you.


Answer: You can use a for loop to iterate over each element, then decide if each
element should be removed. I used the Python doc [ElementTree XML
API](http://docs.python.org/library/xml.etree.elementtree.html) for reference.

    
    
    from xml.etree.ElementTree import ElementTree
    
    tree = ElementTree()
    
    # Test input
    tree.parse("sample.xml")
    
    # List containing names you want to keep
    inputID = ['name1', 'name2', 'name3']
    
    for node in tree.findall('.//data'):
        # Remove node if the name attribute value is not in inputID
        if not node.attrib.get('name') in inputID:
            tree.getroot().remove(node)
    
    # Do what you want with the modified xml
    tree.write('sample_out.xml')
    

