gpt4 book ai didi

python - 使用python将数据从xml存储到数据库

转载 作者:数据小太阳 更新时间:2023-10-29 02:31:03 27 4
gpt4 key购买 nike

1) 我需要将xml文件中的数据保存到数据库中,并在UI中显示保存的数据。

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 来执行上述操作。我是python & xml 的新手。请问有高手帮忙吗。

实际上在我的 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)

我运行了应用程序,但没有得到结果。

最佳答案

JSON 是这类问题的数据库存储的答案。这可能会起作用:

https://github.com/hay/xml2json

python setup.py install

准备出发:

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

关于python - 使用python将数据从xml存储到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15784208/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com