title: Search and replace multiple lines in xml/text files using python
body: <p>---Update 3:
  I have got the script to update the required data into the xml files completed but the following code is being dropped from the written file. Why is this? how can I replace it?</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;?xml-stylesheet type='text/xsl' href='ANZMeta.xsl'?&gt;
  </code></pre>
  
  <p>Current working code (except for issue mentioned above).</p>
  
  <pre><code>import os, xml, arcpy, shutil
  from xml.etree import ElementTree as et 
  
  path=os.getcwd()
  arcpy.env.workspace = path
  
  FileList = arcpy.ListFeatureClasses()
  FileCount = len(FileList)
  zone="_Zone"
  
  for File in FileList:
      FileDesc_obj = arcpy.Describe(File)
      FileNm=FileDesc_obj.file
      newMetaFile=FileNm+"_BaseMetadata.xml"
  
      check_meta=os.listdir(path)
      if FileNm+'.xml' in check_meta:
          shutil.copy2(FileNm+'.xml', newMetaFile)
      else:
          shutil.copy2('L:\Data_Admin\QA\Metadata_python_toolset\Master_Metadata.xml', newMetaFile)
      tree=et.parse(newMetaFile)
  
      print "Processing: "+str(File)
  
      for node in tree.findall('.//title'):
          node.text = str(FileNm)
      for node in tree.findall('.//northbc'):
          node.text = str(FileDesc_obj.extent.YMax)
      for node in tree.findall('.//southbc'):
          node.text = str(FileDesc_obj.extent.YMin)
      for node in tree.findall('.//westbc'):
          node.text = str(FileDesc_obj.extent.XMin)
      for node in tree.findall('.//eastbc'):
          node.text = str(FileDesc_obj.extent.XMax)        
      for node in tree.findall('.//native/nondig/formname'):
          node.text = str(os.getcwd()+"\\"+File)
      for node in tree.findall('.//native/digform/formname'):
          node.text = str(FileDesc_obj.featureType)
      for node in tree.findall('.//avlform/nondig/formname'):
          node.text = str(FileDesc_obj.extension)
      for node in tree.findall('.//avlform/digform/formname'):
          node.text = str(float(os.path.getsize(File))/int(1024))+" KB"
      for node in tree.findall('.//theme'):
          node.text = str(FileDesc_obj.spatialReference.name +" ; EPSG: "+str(FileDesc_obj.spatialReference.factoryCode))
      print node.text
      projection_info=[]
      Zone=FileDesc_obj.spatialReference.name
  
      if "GCS" in str(FileDesc_obj.spatialReference.name):
          projection_info=[FileDesc_obj.spatialReference.GCSName, FileDesc_obj.spatialReference.angularUnitName, FileDesc_obj.spatialReference.datumName, FileDesc_obj.spatialReference.spheroidName]
          print "Geographic Coordinate system"
      else:
          projection_info=[FileDesc_obj.spatialReference.datumName, FileDesc_obj.spatialReference.spheroidName, FileDesc_obj.spatialReference.angularUnitName, Zone[Zone.rfind(zone)-3:]]
          print "Projected Coordinate system"
      x=0
      for node in tree.findall('.//spdom'):
          for node2 in node.findall('.//keyword'):
              print node2.text
              node2.text = str(projection_info[x])
              print node2.text
              x=x+1
  
  
      tree.write(newMetaFile)
  </code></pre>
  
  <p>---Update 1&amp;2:
  Thanks to Aleyna I have the following basic code that works </p>
  
  <pre><code>import os, xml, arcpy, shutil
  from xml.etree import ElementTree as et 
  
  CodeString=['northbc','southbc', '&lt;nondig&gt;&lt;formname&gt;']
  
  nondig='nondigital'
  path=os.getcwd()
  arcpy.env.workspace = path
  xmlfile = path+"\\test.xml"
  
  FileList = arcpy.ListFeatureClasses()
  FileCount = len(FileList)
  
  for File in FileList:
      FileDesc_obj = arcpy.Describe(File)
      FileNm=FileDesc_obj.file
      newMetaFile=FileNm+"_Metadata.xml"
      shutil.copy2('L:\Data_Admin\QA\Metadata_python_toolset\Master_Metadata.xml', newMetaFile)
      tree=et.parse(newMetaFile)
  
      for node in tree.findall('.//northbc'):
          node.text = str(FileDesc_obj.extent.YMax)
      for node in tree.findall('.//southbc'):
          node.text = str(FileDesc_obj.extent.YMin)
      for node in tree.findall('.//westbc'):
          node.text = str(FileDesc_obj.extent.XMin)
      for node in tree.findall('.//eastbc'):
          node.text = str(FileDesc_obj.extent.XMax)        
      for node in tree.findall('.//native/nondig/formname'):
          node.text = nondig
  
      tree.write(newMetaFile)
  </code></pre>
  
  <p>The issue is with dealing with xml code like</p>
  
  <pre><code>- &lt;spdom&gt;
    &lt;keyword thesaurus=""&gt;GDA94&lt;/keyword&gt; 
    &lt;keyword thesaurus=""&gt;GRS80&lt;/keyword&gt; 
    &lt;keyword thesaurus=""&gt;Transverse Mercator&lt;/keyword&gt; 
    &lt;keyword thesaurus=""&gt;Zone 55 (144E - 150E)&lt;/keyword&gt; 
    &lt;/spdom&gt;
  </code></pre>
  
  <p>As keyword thes...is not unique within the <code>&lt;spdom&gt;</code> can we update these in a order from the values coming from </p>
  
  <pre><code>FileDesc_obj.spatialReference.name
  </code></pre>
  
  <blockquote>
    <p>u'GCS_GDA_1994'</p>
  </blockquote>
  
  <p>---ORIGINAL POST---</p>
  
  <p>I am building up a program to generate xml metadata files from spatial files in our library. I have already created the scripts to extract the required spatial and attrib data from the files and create a shp and text file based index of the files but now I want to write this info to base metadata xml file that is written to anzlic standards by replacing the values held by common/static elements...</p>
  
  <p>So for example I want to replace the following xml code</p>
  
  <pre><code>&lt;northbc&gt;8097970&lt;/northbc&gt;
  &lt;southbc&gt;8078568&lt;/southbc&gt;
  </code></pre>
  
  <p>with </p>
  
  <pre><code>&lt;northbc&gt; GeneratedValue_[desc.extent.XMax] /&lt;northbc&gt;
  &lt;southbc&gt; GeneratedValue_[desc.extent.XMax] &lt;/southbc&gt;
  </code></pre>
  
  <p>The issue is that obviously the number/value between  and  will not be the same.</p>
  
  <p>Similarly for xml tags like <code>&lt;title&gt;, &lt;nondig&gt;&lt;formname&gt;</code> etc...in the latter example both tags must be searched for together as formname appears multiple times (is not unique).</p>
  
  <p>I am using the Python Regular Expression manual [here][1], </p>