title: Dump data from <*></*> tags in xml to csv in python (multiple different xml stylesheets formats)
body: <p>--Even after learning a little about XSLT, I didn't use it as the metadata/xls formats change so a single stylesheet based approach won't work ---</p>
  
  <p>I have been trying for the last few hours to grab a csv and dump the data in each tag to a CSV but nothing has worked. I have tried elemtree, parse and regex based on a few other Q&amp;A's in the forum.</p>
  
  <p>For <a href="https://stackoverflow.com/questions/1412004/reading-xml-using-python-minidom-and-iterating-over-each-node">example</a> works fine for his test data but it won't work on my xml (sample at end of question). </p>
  
  <pre><code>tree = ET.parse("test2.xml")
  doc = tree.getroot()
  thingy = doc.find('custod')
  print thingy.attrib
  </code></pre>
  
  <p>Traceback (most recent call last):   File "", line
  1, in  AttributeError: 'NoneType' object has no attribute
  'attrib'</p>
  
  <pre><code>doc
  &lt;Element anzmeta at 801a300&gt;
  thingy = doc.find('anzmeta')
  print thingy.attrib
  </code></pre>
  
  <p>Traceback (most recent call last):   File "", line 1, in  AttributeError: 'NoneType' object has no attribute  'attrib'</p>
  
  <pre><code>doc.attrib
  {}
  </code></pre>
  
  <p>--- Try using <a href="https://stackoverflow.com/questions/7911504/python-string-operation-extract-text-between-html-tags">REX</a></p>
  
  <pre><code>rex = re.compile(r'&lt;custod.*?&gt;(.*?)&lt;/custod&gt;',re.S|re.M)
  rex
  &lt;_sre.SRE_Pattern object at 0x080724A0&gt;
  match=rex.match('test2.xml')
  match
  text = match.groups()[0].strip()
  </code></pre>
  
  <p>Traceback (most recent call last):
    File "", line 1, in 
  AttributeError: 'NoneType' object has no attribute 'groups'</p>
  
  <hr>
  
  <p>All I need is for the system to go through my xml files and create a csv which has the complete entry of each tag in a column of the csv. It should add columns to the csv if they don't exist and then populate them accordingly.</p>
  
  <p>===========
  XML sample</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;?xml-stylesheet type='text/xsl' href='ANZMeta.xsl'?&gt;
  &lt;anzmeta&gt;
    &lt;citeinfo&gt;
      &lt;uniqueid /&gt;
      &lt;title&gt;&amp;lt;&amp;gt;&lt;/title&gt;
      &lt;origin&gt;
        &lt;custod&gt;ATGIS&lt;/custod&gt;
        &lt;jurisdic&gt;
          &lt;keyword thesaurus=""&gt;Tablelands Regional Council&lt;/keyword&gt;
        &lt;/jurisdic&gt;
      &lt;/origin&gt;
    &lt;/citeinfo&gt;
    &lt;descript&gt;
      &lt;abstract&gt;&amp;lt;&amp;gt;
      &lt;/abstract&gt;
      &lt;theme&gt;
        &lt;keyword thesaurus=""&gt;EPSG&lt;/keyword&gt;
      &lt;/theme&gt;
      &lt;spdom&gt;
        &lt;keyword thesaurus=""&gt;GDA94&lt;/keyword&gt;
        &lt;keyword thesaurus=""&gt;GRS80&lt;/keyword&gt;
        &lt;keyword thesaurus=""&gt;Map Grid of Australia&lt;/keyword&gt;
        &lt;keyword thesaurus=""&gt;Zone 55 (144E - 150E)&lt;/keyword&gt;
        &lt;bounding&gt;
          &lt;northbc /&gt;
          &lt;southbc /&gt;
          &lt;eastbc /&gt;
          &lt;westbc /&gt;
        &lt;/bounding&gt;
      &lt;/spdom&gt;
    &lt;/descript&gt;
    &lt;timeperd&gt;
      &lt;begdate&gt;
        &lt;date&gt;2012&lt;/date&gt;
      &lt;/begdate&gt;
      &lt;enddate&gt;
        &lt;keyword thesaurus=""&gt;Completed&lt;/keyword&gt;
      &lt;/enddate&gt;
    &lt;/timeperd&gt;
    &lt;status&gt;
      &lt;progress&gt;
        &lt;keyword thesaurus=""&gt;Ongoing&lt;/keyword&gt;
        &lt;keyword thesaurus=""&gt;Completed&lt;/keyword&gt;
      &lt;/progress&gt;
      &lt;update&gt;
        &lt;keyword thesaurus=""&gt;As Required&lt;/keyword&gt;
        &lt;keyword thesaurus=""&gt;As Required&lt;/keyword&gt;
      &lt;/update&gt;
    &lt;/status&gt;
    &lt;distinfo&gt;
      &lt;native&gt;
        &lt;nondig&gt;
          &lt;formname&gt;File&lt;/formname&gt;
        &lt;/nondig&gt;
        &lt;digform&gt;
          &lt;formname&gt;Type:&lt;/formname&gt;
        &lt;/digform&gt;
      &lt;/native&gt;
      &lt;avlform&gt;
        &lt;nondig&gt;
          &lt;formname&gt;Format:&lt;/formname&gt;
        &lt;/nondig&gt;
        &lt;digform&gt;
          &lt;formname&gt;Size&lt;/formname&gt;
        &lt;/digform&gt;
      &lt;/avlform&gt;
      &lt;accconst&gt;Internal Use Only&lt;/accconst&gt;
    &lt;/distinfo&gt;
    &lt;dataqual&gt;
      &lt;lineage&gt;~TBC~&lt;/lineage&gt;
      &lt;procstep&gt;
        &lt;procdesc Sync="TUE"&gt;Metadata imported.&lt;/procdesc&gt;
        &lt;srcused Sync="TRUE"&gt;L:\Data_Admin\MetadataGenerator\trc_Metadata_Template.xml&lt;/srcused&gt;
        &lt;date Sync="TRUE"&gt;20121206&lt;/date&gt;
        &lt;time Sync="TRUE"&gt;15341400&lt;/time&gt;
      &lt;/procstep&gt;
      &lt;posacc&gt;~TBC~&lt;/posacc&gt;
      &lt;attracc&gt;~TBC~&lt;/attracc&gt;
      &lt;logic&gt;~TBC~&lt;/logic&gt;
      &lt;complete&gt;~TBC~&lt;/complete&gt;
    &lt;/dataqual&gt;
    &lt;cntinfo&gt;
      &lt;cntorg&gt;Atherton Tablelands GIS&lt;/cntorg&gt;
      &lt;cntpos&gt;GIS Coordinator&lt;/cntpos&gt;
      &lt;address&gt;PO Box 1616, 8 Tolga Rd&lt;/address&gt;
      &lt;city&gt;Atherton&lt;/city&gt;
      &lt;state&gt;QLD&lt;/state&gt;
      &lt;country&gt;AUSTRALIA&lt;/country&gt;
      &lt;postal&gt;4883&lt;/postal&gt;
      &lt;cntvoice&gt;07 40918600&lt;/cntvoice&gt;
      &lt;cntfax&gt;07 40917035&lt;/cntfax&gt;
      &lt;cntemail&gt;info@atgis.com.au&lt;/cntemail&gt;
    &lt;/cntinfo&gt;
    &lt;metainfo&gt;
      &lt;metd&gt;
        &lt;date /&gt;
      &lt;/metd&gt;
    &lt;/metainfo&gt;
  &lt;/anzmeta&gt;
  </code></pre>
  
  <p>--- Start of my script</p>
  
  <pre><code>import os, xml, shutil, datetime
  from xml.etree import ElementTree as et
  
  SourceDIR=os.getcwd()
  outDIR=os.getcwd()+'//out'
  
  def locatexml(SourceDIR,outDIR):
      xmllist=[]
      for root, dirs, files in os.walk(SourceDIR, topdown=False):
          for fl in files:
              currentFile=os.path.join(root, fl)
              ext=fl[fl.rfind('.')+1:]
              if ext=='xml':
                  xmllist.append(currentFile)
                  print currentFile
                  readxml(currentFile)
      print "finished"
      return xmllist
  
  def readxml(currentFile):
      tree=et.parse(currentFile)
      print "Processing: "+str(currentFile)
  
  locatexml(SourceDIR,outDIR)
  print xmllist
  </code></pre>