Tiếp theo là file XML: book.xmlLàm thế nào để lưu trữ hiệu quả tài liệu XML được phân tích cú pháp này trong Cơ sở dữ liệu MySQL bằng Python?
<?xml version="1.0" ?>
<!--Sample XML Document-->
<bookstore>
<book _id="E7854">
<title>
Sample XML Book
</title>
<author>
<name _id="AU363">
<first>
Benjamin
</first>
<last>
Smith
</last>
</name>
<affiliation>
A
</affiliation>
</author>
<chapter number="1">
<title>
First Chapter
</title>
<para>
B
<count>
783
</count>
.
</para>
</chapter>
<chapter number="3">
<title>
Third Chapter
</title>
<para>
B
<count>
59
</count>
.
</para>
</chapter>
</book>
<book _id="C843">
<title>
XML Master
</title>
<author>
<name _id="AU245">
<first>
John
</first>
<last>
Doe
</last>
</name>
<affiliation>
C
</affiliation>
</author>
<chapter number="2">
<title>
Second Chapter
</title>
<para>
K
<count>
54
</count>
.
</para>
</chapter>
<chapter number="3">
<title>
Third Chapter
</title>
<para>
K
<count>
328
</count>
.
</para>
</chapter>
<chapter number="7">
<title>
Seventh Chapter
</title>
<para>
K
<count>
265
</count>
.
</para>
</chapter>
<chapter number="9">
<title>
Ninth Chapter
</title>
<para>
K
<count>
356
</count>
.
</para>
</chapter>
</book>
</bookstore>
Sau đây là mã Python: book_dom.py
from xml.dom import minidom, Node
import re, textwrap
class SampleScanner:
def __init__(self, doc):
for child in doc.childNodes:
if child.nodeType == Node.ELEMENT_NODE and child.tagName == 'bookstore':
self.handleBookStore(child)
def gettext(self, nodelist):
retlist = []
for node in nodelist:
if node.nodeType == Node.TEXT_NODE:
retlist.append(node.wholeText)
elif node.hasChildNodes:
retlist.append(self.gettext(node.childNodes))
return re.sub('\s+', ' ', ''.join(retlist))
def handleBookStore(self, node):
for child in node.childNodes:
if child.nodeType != Node.ELEMENT_NODE:
continue
if child.tagName == 'book':
self.handleBook(child)
def handleBook(self, node):
for child in node.childNodes:
if child.nodeType != Node.ELEMENT_NODE:
continue
if child.tagName == 'title':
print "Book title is:", self.gettext(child.childNodes)
if child.tagName == 'author':
self.handleAuthor(child)
if child.tagName == 'chapter':
self.handleChapter(child)
def handleAuthor(self, node):
for child in node.childNodes:
if child.nodeType != Node.ELEMENT_NODE:
continue
if child.tagName == 'name':
self.handleAuthorName(child)
elif child.tagName == 'affiliation':
print "Author affiliation:", self.gettext([child])
def handleAuthorName(self, node):
surname = self.gettext(node.getElementsByTagName("last"))
givenname = self.gettext(node.getElementsByTagName("first"))
print "Author Name: %s, %s" % (surname, givenname)
def handleChapter(self, node):
print " *** Start of Chapter %s: %s" % (node.getAttribute('number'),
self.gettext(node.getElementsByTagName('title')))
for child in node.childNodes:
if child.nodeType != Node.ELEMENT_NODE:
continue
if child.tagName == 'para':
self.handlePara(child)
def handlePara(self, node):
partext = self.gettext([node])
partext = textwrap.fill(partext)
print partext
print
doc = minidom.parse('book.xml')
SampleScanner(doc)
Output: ~/$ python book_dom.py
Book ID : E7854
Book title is: Sample XML Book
Name ID : AU363
Author Name: Smith , Benjamin
Author affiliation: A
*** Start of Chapter 1: First Chapter
B 783 .
*** Start of Chapter 3: Third Chapter
B 59 .
Book ID : C843
Book title is: XML Master
Name ID : AU245
Author Name: Doe , John
Author affiliation: C
*** Start of Chapter 2: Second Chapter
K 54 .
*** Start of Chapter 3: Third Chapter
K 328 .
*** Start of Chapter 7: Seventh Chapter
K 265 .
*** Start of Chapter 9: Ninth Chapter
K 356 .
Mục đích của tôi là lưu trữ các Sách trong bảng "Sách" và Thông tin tác giả trong bảng "Tác giả" (bảo quản sách -> mối quan hệ tác giả) [MySQL DB].
**Book table :**
id |title
E7854 Sample XML Book
....
**Chapter table :**
book_id|chapter_number|title |para
E7854 1 First Chapter B 783 .
E7854 3 Third Chapter B 59 .
....
**Author table :**
id |book_id |name |Affiliation
AU363 E7854 Smith Benjamin A
....
Làm cách nào để lưu trữ dữ liệu trong cơ sở dữ liệu nếu tôi có vài nghìn sách và tác giả (và chương)? Tôi gặp sự cố khi xác định duy nhất tập dữ liệu cho từng cuốn sách/tác giả. Tôi có thể sử dụng các ID và chuyển chúng đến các chức năng để duy trì mối quan hệ nhưng tôi không chắc đó có phải là cách tốt nhất để làm điều đó hay không. Bất kỳ con trỏ nào được đánh giá cao.
p.s: Tôi đang làm việc trên phần SQL của tập lệnh và sẽ cập nhật khi tôi kiểm tra nó. Cảm thấy tự do để đăng suy nghĩ của bạn, mẫu mã. Cảm ơn!
Câu hỏi hay. –
Đây là một câu hỏi thú vị, nhưng tôi không chắc chắn rằng câu hỏi * thực tế là rõ ràng. Bạn đang tìm kiếm hướng dẫn cho thiết kế cơ sở dữ liệu quan hệ, hay bạn muốn trợ giúp mã để dịch XML của bạn sang thiết kế cơ sở dữ liệu hiện có của bạn? – syrion
Tôi đang tìm kiếm trợ giúp với mã Python để theo dõi từng bản ghi duy nhất để lưu trữ trong cơ sở dữ liệu. Ngay cả một dict Python hoặc danh sách sẽ giúp - Tôi gặp khó khăn khi cố gắng phân biệt mỗi bản ghi khi tôi phân tích cú pháp mỗi nút. – ThinkCode