title: Python ElementTree XML Parsing
body: <p>I am trying to parse an xml file that I got by exporting a pdf to xml 1.0 using adobe pro.
  I am using Python and ElementTree to parse with.
  The pdf contains a table which spans multiple pages and has several different table headers.</p>
  
  <p>I want to parse and extract the row and column data from the table which begins with the header that contains a particular string (e.g. "MECHANICAL") and stop at the next table heading section (e.g. "COMPLETED").  Thereby excluding all row and column data before and after this section.  There is no easy tag to parse, the tag pattern just repeats.</p>
  
  <p>Here is my current python code:</p>
  
  <pre><code># Python
  
  import sys
  import re     # regular expression
  import xml.etree.ElementTree as xml
  
  tree = xml.parse("C:/Documents and Settings/alilly.CORPORATE/Desktop/python xml parse/excerpt.xml")
  
  print "=================== Find Columns ===================="    
  
  for node in tree.iter('TR'):
  
      print "tag=",node.tag
  
      count = len(node.getiterator('TD'))
  
      #if count != 10:
      #    continue
  
      print "------------"
  
      for col in node.getiterator('TD'):
          print "      tag=",col.tag, "attrib=", col.attrib, "text=", col.text
  
  
  print "=================== Find Headers ===================="
  
  # find headers
  for node in tree.iter('ImageData'):
      print "figure text = ", node.tail
  </code></pre>
  
  <p>And here is my XML file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!-- Created from PDF via Acrobat SaveAsXML --&gt;
  &lt;!-- Mapping Table version: 28-February-2003 --&gt;
  &lt;TaggedPDF-doc&gt;
  &lt;?xpacket begin='?' id='W5M0MpCehiHzreSzNTczkc9d'?&gt;
  &lt;?xpacket begin="?" id="W5M0MpCehiHzreSzNTczkc9d"?&gt;
  &lt;x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.1-c041 52.342996, 2008/05/07-20:48:00        "&gt;
     &lt;rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"&gt;
        &lt;rdf:Description rdf:about=""
              xmlns:pdf="http://ns.adobe.com/pdf/1.3/"&gt;
           &lt;pdf:Producer&gt;GPL Ghostscript 8.70&lt;/pdf:Producer&gt;
           &lt;pdf:Keywords/&gt;
        &lt;/rdf:Description&gt;
        &lt;rdf:Description rdf:about=""
              xmlns:xmp="http://ns.adobe.com/xap/1.0/"&gt;
           &lt;xmp:ModifyDate&gt;2011-03-01T09:36:13-05:00&lt;/xmp:ModifyDate&gt;
           &lt;xmp:CreateDate&gt;2011-03-01T09:36:13-05:00&lt;/xmp:CreateDate&gt;
           &lt;xmp:CreatorTool&gt;PDFCreator Version 1.0.2&lt;/xmp:CreatorTool&gt;
        &lt;/rdf:Description&gt;
        &lt;rdf:Description rdf:about=""
              xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/"&gt;
           &lt;xmpMM:DocumentID&gt;d417764e-466c-11e0-0000-f7ea6a538d79&lt;/xmpMM:DocumentID&gt;
           &lt;xmpMM:InstanceID&gt;uuid:0c6ada50-6db0-4d59-88e1-fc23aa6ebc14&lt;/xmpMM:InstanceID&gt;
        &lt;/rdf:Description&gt;
        &lt;rdf:Description rdf:about=""
              xmlns:dc="http://purl.org/dc/elements/1.1/"&gt;
           &lt;dc:format&gt;xml&lt;/dc:format&gt;
           &lt;dc:title&gt;
              &lt;rdf:Alt&gt;
                 &lt;rdf:li xml:lang="x-default"&gt;my pdf file&lt;/rdf:li&gt;
              &lt;/rdf:Alt&gt;
           &lt;/dc:title&gt;
           &lt;dc:creator&gt;
              &lt;rdf:Seq&gt;
                 &lt;rdf:li&gt;ltamm&lt;/rdf:li&gt;
              &lt;/rdf:Seq&gt;
           &lt;/dc:creator&gt;
           &lt;dc:description&gt;
              &lt;rdf:Alt&gt;
                 &lt;rdf:li xml:lang="x-default"/&gt;
                 &lt;rdf:li xml:lang="x-repair"/&gt;
              &lt;/rdf:Alt&gt;
           &lt;/dc:description&gt;
        &lt;/rdf:Description&gt;
     &lt;/rdf:RDF&gt;
  &lt;/x:xmpmeta&gt;
  &lt;?xpacket end="w"?&gt;
  &lt;?xpacket end='r'?&gt;
  &lt;Part&gt;
  &lt;H1&gt;Misc &lt;/H1&gt;
  &lt;Sect&gt;
  &lt;H3&gt;This is a test &lt;/H3&gt;
  &lt;Sect&gt;
  &lt;H5&gt;Deletions &lt;/H5&gt;
  &lt;L&gt;
  &lt;LI&gt;
  &lt;LI_Title&gt;Special codes &lt;/LI_Title&gt;
  &lt;/LI&gt;
  &lt;/L&gt;
  &lt;Figure&gt;
  &lt;ImageData src=""/&gt;
  &lt;/Figure&gt;
  &lt;Figure&gt;
  &lt;ImageData src=""/&gt;
  Main INTERIOR &lt;/Figure&gt;
  &lt;Table&gt;
  &lt;TR&gt;
  &lt;TH&gt;S = Standard O = Optional &lt;/TH&gt;
  &lt;/TR&gt;
  &lt;TR&gt;
  &lt;TD&gt;&lt;Figure&gt;
  &lt;ImageData src=""/&gt;
  &lt;/Figure&gt;
  &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;/TR&gt;
  &lt;/Table&gt;
  &lt;Figure&gt;
  &lt;ImageData src=""/&gt;
  This is the MECHANICAL header&lt;/Figure&gt;
  &lt;Table&gt;
  &lt;TR&gt;
  &lt;TH&gt;S = Standard O = Optional &lt;/TH&gt;
  &lt;/TR&gt;
  &lt;TR&gt;
  &lt;TH&gt;Free Flow &lt;/TH&gt;
  &lt;TD&gt;Ref. Code &lt;/TD&gt;
  &lt;TD&gt;DESCRIPTION &lt;/TD&gt;
  &lt;TD&gt;Rooster &lt;/TD&gt;
  &lt;TD&gt;747 Dog &lt;/TD&gt;
  &lt;TD&gt;888 Rabbit &lt;/TD&gt;
  &lt;/TR&gt;
  &lt;TR&gt;
  &lt;TD&gt;xxx GOgo xxB &lt;/TD&gt;
  &lt;TD&gt;Beany xxx &lt;/TD&gt;
  &lt;TD&gt;nothing here xxx &lt;/TD&gt;
  &lt;TD&gt;xxx B &lt;/TD&gt;
  &lt;TD&gt;snake ddd &lt;/TD&gt;
  &lt;TD&gt;Cow fff &lt;/TD&gt;
  &lt;TD&gt;eee &lt;/TD&gt;
  &lt;/TR&gt;
  &lt;TR&gt;
  &lt;TH/&gt;
  &lt;TD/&gt;
  &lt;TD&gt;Squirrel Protection &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;/TR&gt;
  &lt;TR&gt;
  &lt;TH/&gt;
  &lt;TD&gt;J77 &lt;/TD&gt;
  &lt;TD&gt;Rocket Launcher &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;/TR&gt;
  &lt;TR&gt;
  &lt;TH/&gt;
  &lt;TD/&gt;
  &lt;TD&gt;Lunch &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;TD&gt;S &lt;/TD&gt;
  &lt;/TR&gt;
  &lt;TR&gt;
  &lt;TH/&gt;
  &lt;TD&gt;Jss5 &lt;/TD&gt;
  &lt;TD&gt;Now is the time for all good men &lt;/TD&gt;
  &lt;TD&gt;-&lt;/TD&gt;
  &lt;TD&gt;A1 &lt;/TD&gt;
  &lt;TD&gt;A1 &lt;/TD&gt;
  &lt;TD&gt;-&lt;/TD&gt;
  &lt;TD&gt;-&lt;/TD&gt;
  &lt;TD&gt;-&lt;/TD&gt;
  &lt;TD&gt;-&lt;/TD&gt;
  &lt;/TR&gt;
  &lt;TR&gt;
  &lt;TD&gt;Capacity &lt;/TD&gt;
  &lt;TD/&gt;
  &lt;TD&gt;2/3 &lt;/TD&gt;
  &lt;TD&gt;2/3 &lt;/TD&gt;
  &lt;TD&gt;2/3 &lt;/TD&gt;
  &lt;/TR&gt;
  &lt;/Table&gt;
  &lt;Figure&gt;
  &lt;ImageData src=""/&gt;
  Final COMPLETED PAGE 1 OF 2 &lt;/Figure&gt;
  &lt;Figure&gt;
  &lt;ImageData src=""/&gt;
  &lt;/Figure&gt;
  &lt;P&gt;Graphite &lt;/P&gt;
  &lt;P&gt;painted fun &lt;/P&gt;
  &lt;P&gt;Control yourself &lt;/P&gt;
  &lt;Figure&gt;
  &lt;ImageData src=""/&gt;
  Meaningless Header PAGE 2 OF 2 &lt;/Figure&gt;
  &lt;Figure&gt;
  &lt;ImageData src=""/&gt;
  &lt;/Figure&gt;
  &lt;P&gt;)multi-coat &lt;/P&gt;
  &lt;P&gt;front&lt;/P&gt;
  &lt;P&gt;single-slot system &lt;/P&gt;
  &lt;Figure&gt;
  &lt;ImageData src=""/&gt;
  Almost Done Header PAGE 1 OF 1 &lt;/Figure&gt;
  &lt;Figure&gt;
  &lt;ImageData src=""/&gt;
  &lt;/Figure&gt;
  &lt;Figure&gt;
  &lt;ImageData src=""/&gt;
  &lt;/Figure&gt;
  &lt;Figure&gt;
  &lt;ImageData src=""/&gt;
  &lt;/Figure&gt;
  &lt;P&gt;Snow Blizzard. &lt;/P&gt;
  &lt;P&gt;Done &lt;/P&gt;
  &lt;/Sect&gt;
  &lt;/Sect&gt;
  &lt;/Part&gt;
  &lt;/TaggedPDF-doc&gt;
  </code></pre>