title: Use lxml to read xml and compare elements
body: <p>I've tried searching various questions and answers here on StackOverflow and cannot find a solution that works for my situation, so here is my issue.</p>
  
  <p>I have 3 xml files that I am attempting to compare. The issue I am having is grabbing sections of the "Main" XML file at a time and keeping the information together. For example, I want to keep the information associated with 1 and be able to use each piece within the script.</p>
  
  <p>This XML file can have any number of fields between the   tags but I am only needing 5 specific fields. I am fairly new to Python and extremely new to using Python to read more than a text file, any help would be appreciated.</p>
  
  <p>A sample of the xml is below.</p>
  
  <pre><code>Main XML:
      &lt;?xml version="1.0" encoding="ISO-8859-1" ?&gt;
      &lt;resultset table="foo_bar"&gt;
      &lt;row&gt;
          &lt;field name="id"&gt;1&lt;/field&gt;
          &lt;field name="name"&gt;foo 1&lt;/field&gt;
          &lt;field name="item 1"&gt;bar 1&lt;/field&gt;
          &lt;field name="item 2"&gt;Accepted&lt;/field&gt;
          &lt;field name="item 3"&gt;Accepted&lt;/field&gt;
      &lt;/row&gt;
      &lt;row&gt;
          &lt;field name="id"&gt;2&lt;/field&gt;
          &lt;field name="name"&gt;foo 2&lt;/field&gt;
          &lt;field name="item 1"&gt;bar 2&lt;/field&gt;
          &lt;field name="item 2"&gt;Declined&lt;/field&gt;
          &lt;field name="item 3"&gt;Accepted&lt;/field&gt;
      &lt;/row&gt;
      &lt;row&gt;
          &lt;field name="id"&gt;3&lt;/field&gt;
          &lt;field name="name"&gt;foo 3&lt;/field&gt;
          &lt;field name="item 1"&gt;bar 3&lt;/field&gt;
          &lt;field name="item 2"&gt;Accepted&lt;/field&gt;
          &lt;field name="item 3"&gt;Declined&lt;/field&gt;
      &lt;/row&gt;
      .....Continues
      &lt;/resultset&gt;
  </code></pre>
  
  <p>I have tried following the various answers for similar questions, but have had no success thus far.</p>
  
  <p><strong>EDIT</strong> I've tried multiple things, I'll have to dig through the various .py scripts to find all of them. Here is the most recent based on the Question posted <a href="https://stackoverflow.com/questions/15168259/python-xml-remove-some-elements-and-their-children-but-keep-specific-elements-an">here</a></p>
  
  <pre><code>from lxml import etree as ET
  
  def filter_by_itemid(doc, idlist):
      rowset = doc.xpath("//row")
      for elem in rowset.getchildren():
          if elem.get("*") not in idlist:
              rowset.remove(elem)
      return doc
  
  doc = ET.parse("my.xml")
  filter_by_itemid(doc, ['id', 'name', 'item 1', 'item 2', 'item 3'])
  
  print(ET.tostring(doc))
  </code></pre>
  
  <p>I know I am doing something wrong somewhere, and the formatting of the xml (which I am unable to change at the source) isn't helping...</p>
  
  <p>The error I receive is "AttributeError: 'list' object has no attribute 'getchildren' "</p>