Minimal DOM-Implementierung:
-Link: http://docs.python.org/2/library/xml.dom.minidom.html#module-xml.dom.minidom
Python liefert eine vollständige, W3C-Standard-Implementierung von XML-DOM (xml.dom) und eine minimale, xml.dom.minidom. Dieser letztere ist einfacher und kleiner als die vollständige Implementierung. Aus einer "Parsing-Perspektive" hat es jedoch alle Vor- und Nachteile des Standard-DOM - d. H. Es lädt alles im Speicher.
eine einfache XML-Datei unter Berücksichtigung
<?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>
Ein möglicher Python-Parser minidom ist:
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
Beachten Sie, dass xml.parsers.expat ist eine Python-Schnittstelle zum Expat nicht validierender XML-Parser (docs.python.org/2/library/pyexpat.html).
Die xml.dom Paket liefert auch die Ausnahmeklasse DOMException, aber es ist nicht in minidom supperted!
Der ElementTree XML API:
-Link: http://docs.python.org/2/library/xml.etree.elementtree.html
ElementTree ist viel einfacher zu bedienen und erfordert weniger Speicher als XML DOM. Außerdem ist eine C-Implementierung verfügbar (xml.etree.cElementTree).
Ein möglicher Python-Parser ElementTree verwendet, ist: nicht eine Reihe von Veranstaltungen
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
Vielen Dank, dass Sie die beiden folgenden Vorbehalte erwähnt haben! (Ich brauche beide in meinem Projekt.) "XPath-Unterstützung ... ElementTree weicht von DOM ab, wo Knoten Zugriff auf ihre Eltern und Geschwister haben." –