Minimal implementacja DOM:
Link: http://docs.python.org/2/library/xml.dom.minidom.html#module-xml.dom.minidom
Python dostarcza pełny, W3C-standardowego wdrożenia XML DOM (xml.dom) i minimalną, xml.dom.minidom. Ten ostatni jest prostszy i mniejszy niż pełne wdrożenie. Jednak z perspektywy "analizowania" ma ona wszystkie zalety i wady standardowego DOM - tj. Ładuje wszystko w pamięci.
Biorąc pod uwagę podstawowy plik XML:
<?xml version="1.0"?>
<catalog>
<book isdn="xxx-1">
<author>A1</author>
<title>T1</title>
</book>
<book isdn="xxx-2">
<author>A2</author>
<title>T2</title>
</book>
</catalog>
Możliwym parser Python korzystając minidom jest:
import os
from xml.dom import minidom
from xml.parsers.expat import ExpatError
#-------- Select the XML file: --------#
#Current file name and directory:
curpath = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(curpath, "sample.xml")
#print "Filename: %s" % (filename)
#-------- Parse the XML file: --------#
try:
#Parse the given XML file:
xmldoc = minidom.parse(filepath)
except ExpatError as e:
print "[XML] Error (line %d): %d" % (e.lineno, e.code)
print "[XML] Offset: %d" % (e.offset)
raise e
except IOError as e:
print "[IO] I/O Error %d: %s" % (e.errno, e.strerror)
raise e
else:
catalog = xmldoc.documentElement
books = catalog.getElementsByTagName("book")
for book in books:
print book.getAttribute('isdn')
print book.getElementsByTagName('author')[0].firstChild.data
print book.getElementsByTagName('title')[0].firstChild.data
Zauważ, że xml.parsers.expat to interfejs Pythona do Expat nie sprawdzający poprawności analizator składni XML (docs.python.org/2/library/pyexpat.html).
W xml.dom materiały pakietowe również klasa wyjątek DOMException, ale to nie jest supperted w minidom!
ElementTree XML API:
Link: http://docs.python.org/2/library/xml.etree.elementtree.html
ElementTree jest znacznie łatwiejszy w użyciu i wymaga mniej pamięci niż XML DOM. Ponadto dostępne jest implementacja C (xml.etree.cElementTree).
Możliwym parser Python stosując ElementTree jest:
import os
from xml.etree import cElementTree # C implementation of xml.etree.ElementTree
from xml.parsers.expat import ExpatError # XML formatting errors
#-------- Select the XML file: --------#
#Current file name and directory:
curpath = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(curpath, "sample.xml")
#print "Filename: %s" % (filename)
#-------- Parse the XML file: --------#
try:
#Parse the given XML file:
tree = cElementTree.parse(filename)
except ExpatError as e:
print "[XML] Error (line %d): %d" % (e.lineno, e.code)
print "[XML] Offset: %d" % (e.offset)
raise e
except IOError as e:
print "[XML] I/O Error %d: %s" % (e.errno, e.strerror)
raise e
else:
catalogue = tree.getroot()
for book in catalogue:
print book.attrib.get("isdn")
print book.find('author').text
print book.find('title').text
Dziękujemy za podanie obu poniższych zastrzeżeń! (Potrzebuję obu w moim projekcie.) "Obsługa XPath ... ElementTree różni się od DOM, gdzie węzły mają dostęp do rodzica i rodzeństwa." –