3

1) I need to save the data from xml file to database and display the saved data in UI.

2) I am using mysql for database.

my xml file is

<!-- books.xml -->
<catalog>
  <book isbn="1-880985-26-8">
    <title>The Consumer</title>
    <author>M. Gira</author>
  </book>
  <book isbn="0-679775-43-9">
    <title>The Wind-Up Bird Chronicle</title>
    <author>Haruki Murakami</author>
  </book>
  <book isbn="0-679775-13-6">
    <title>Deccon Chronicle</title>
    <author>Kulkarni</author>
  </book>
  <book isbn="0-679775-93-6">
    <title>Python</title>
    <author>David varner</author>
  </book>
</catalog>

How to write views.py or filename.py to perform the above operation.I am new to python & xml.Can i get the help from experts.

actually in my bookhandler.py i did this,

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 = ""

my models.py for storing data is

class Book(models.Model):
    ISBN=models.AutoField(primary_key=True,unique=True)
    title=models.CharField(max_length=30)
    author=models.CharField(max_length=40)

I runned the app,but i am not getting the result.

4
  • take a look at docs.python.org/2/library/xml.etree.elementtree.html and some MySQL connector for python Commented Apr 3, 2013 at 10:04
  • ya sir,i already referred that but in practical i am confused to implement.So a live example will help me more to understand Commented Apr 3, 2013 at 10:07
  • @Benjamin what i tried was edited,please see what is wrong with that code Commented Apr 3, 2013 at 10:25
  • can i get the answer for the above question Commented Apr 3, 2013 at 10:54

1 Answer 1

4

JSON is the answer for database storage on that kind of issue. This probably will work:

https://github.com/hay/xml2json

python setup.py install

And ready to go:

import xml2json
import json

s = '''<?xml version="1.0"?>
<catalog>
  <book isbn="1-880985-26-8">
    <title>The Consumer</title>
    <author>M. Gira</author>
  </book>
  <book isbn="0-679775-43-9">
    <title>The Wind-Up Bird Chronicle</title>
    <author>Haruki Murakami</author>
  </book>
  <book isbn="0-679775-13-6">
    <title>Deccon Chronicle</title>
    <author>Kulkarni</author>
  </book>
  <book isbn="0-679775-93-6">
    <title>Python</title>
    <author>David varner</author>
  </book>
</catalog>'''

### Storage data:
print xml2json.xml2json(s)

### Parsing to use:
json_data = json.loads(xml2json.xml2json(s))
Sign up to request clarification or add additional context in comments.

2 Comments

Oswaldo Ferreira just tell me,while doing the above operation wheather we need to make any changes in views.py
@MonkL You seem to be doing everything right. Are you asking the question specifically for django, in that case i have a feeling you are looking at the idea of writing a management command. You should check this docs.djangoproject.com/en/dev/howto/custom-management-commands which will let you write a shell command. To be able to help you would have to tell what your end goal is though for which you need to write views or filename.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.