title: storing data from xml to database using python
body: <p>1) I need to save the data from xml file to database and display the saved data in UI.</p>
  
  <p>2) I am using mysql for database.</p>
  
  <p>my xml file is </p>
  
  <pre><code>&lt;!-- books.xml --&gt;
  &lt;catalog&gt;
    &lt;book isbn="1-880985-26-8"&gt;
      &lt;title&gt;The Consumer&lt;/title&gt;
      &lt;author&gt;M. Gira&lt;/author&gt;
    &lt;/book&gt;
    &lt;book isbn="0-679775-43-9"&gt;
      &lt;title&gt;The Wind-Up Bird Chronicle&lt;/title&gt;
      &lt;author&gt;Haruki Murakami&lt;/author&gt;
    &lt;/book&gt;
    &lt;book isbn="0-679775-13-6"&gt;
      &lt;title&gt;Deccon Chronicle&lt;/title&gt;
      &lt;author&gt;Kulkarni&lt;/author&gt;
    &lt;/book&gt;
    &lt;book isbn="0-679775-93-6"&gt;
      &lt;title&gt;Python&lt;/title&gt;
      &lt;author&gt;David varner&lt;/author&gt;
    &lt;/book&gt;
  &lt;/catalog&gt;
  </code></pre>
  
  <p>How to write views.py or filename.py to perform the above operation.I am new to python &amp; xml.Can i get the help from experts.</p>
  
  <p>actually in my bookhandler.py i did this,</p>
  
  <pre><code>from sqlalchemy import *
  from sqlalchemy.orm import *
  
  import xml.sax.handler
  
  pg_db = create_engine('postgres:///testdb?user=homer')
  
  metadata = MetaData(pg_db)
  
  books_table = Table('books', metadata, autoload=True)
  
  class Book(object):
      pass
  
  mapper(Book, books_table)
  
  class BookHandler(xml.sax.handler.ContentHandler):
      def __init__(self):
          self.buffer = ""
          self.inField = 0
          self.session = create_session(bind=pg_db)
  
      def startElement(self, name, attributes):
          if name == "book":
              self.isbn = attributes["isbn"]
          elif name == "title":
              self.inField = 1
          elif name == "author":
              self.inField = 1
  
      def characters(self, data):
          if self.inField:
              self.buffer += data
  
      def endElement(self, name):
          if name == "book":
              self.session.begin()
              self.newbook = Book()
              self.newbook.isbn = self.isbn
              self.newbook.title = self.title
              self.newbook.author = self.author
              self.session.save(self.newbook)
              self.session.commit()
          elif name == "title":
              self.inField = 0
              self.title = self.buffer
          elif name == "author":
              self.inField = 0
              self.author = self.buffer
          self.buffer = ""
  </code></pre>
  
  <p>my models.py for storing data is </p>
  
  <pre><code>class Book(models.Model):
      ISBN=models.AutoField(primary_key=True,unique=True)
      title=models.CharField(max_length=30)
      author=models.CharField(max_length=40)
  </code></pre>
  
  <p>I runned the app,but i am not getting the result.</p>