title: Extract elements from XML file using Python
body: <p>The link below gives us the list of ingredients in recipelist. I would like to extract the names of the ingredient and save it to another file using python.
  <a href="http://stream.massey.ac.nz/file.php/6087/Eva_Material/Tutorials/recipebook.xml" rel="nofollow">http://stream.massey.ac.nz/file.php/6087/Eva_Material/Tutorials/recipebook.xml</a></p>
  
  <p>So far I have tried using the following code, but it gives me the complete recipe not the names of the ingredients:</p>
  
  <pre><code>from xml.sax.handler import ContentHandler
  import xml.sax
  import sys
  def recipeBook(): 
      path = "C:\Users\user\Desktop"
      basename = "recipebook.xml"
      filename = path+"\\"+basename
      file=open(filename,"rt")
      # find contents 
      contents = file.read()
  
      class textHandler(ContentHandler):
        def characters(self, ch):
        sys.stdout.write(ch.encode("Latin-1"))
      parser = xml.sax.make_parser()
      handler = textHandler( )
      parser.setContentHandler(handler)
      parser.parse("C:\Users\user\Desktop\\recipebook.xml")
  
  
  
    file.close()
  </code></pre>
  
  <p>How do I extract the name of each ingredient and save them to another file?</p>