2013-04-03 141 views
2

1)我需要將xml文件中的數據保存到數據庫,並在UI中顯示保存的數據。使用python將數據從xml存儲到數據庫

2)我正在使用mysql的數據庫。

我的XML文件

<!-- 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> 

如何寫views.py或filename.py執行上述operation.I是新來的蟒蛇& xml.Can我得到專家的幫助。

居然在我bookhandler.py我這樣做,

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

我用於存儲數據models.py是

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

我拼命地跑的應用程序,但我沒有得到結果。

+0

看看在http://docs.python。 org/2/library/xml.etree.elementtree.html和python的一些MySQL連接器 –

+0

ya先生,我已經提到,但實際上我很困惑實現。所以一個活的例子w生病幫助我更多瞭解 –

+0

@Benjamin我試了編輯,請看看代碼有什麼問題 –

回答

1

JSON是數據庫存儲在這類問題上的答案。這可能會工作:

https://github.com/hay/xml2json

蟒蛇的setup.py安裝

,並準備去:

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)) 
+0

Oswaldo Ferreira告訴我,在做上述操作時,我們需要做任何更改views.py –

+0

@MonkL你似乎在做一切正確。你是否特意爲django提出了這個問題,在那種情況下,我有一種感覺,你正在考慮編寫管理命令的想法。你應該檢查這個https://docs.djangoproject.com/en/dev/howto/custom-management-commands/,它可以讓你編寫一個shell命令。爲了能夠幫助你必須告訴你的最終目標是什麼,但你需要編寫視圖或文件名.py – dusual

+0

ya offcourse,我正在做django。 –

相關問題