<catalog>;
<book isbn="1-56592-724-9">;
<title>;The Cathedral & the Bazaar</title>;
<author>;Eric S. Raymond</author>;
</book>;
<book isbn="1-56592-051-1">;
<title>;Making TeX Work</title>;
<author>;Norman Walsh</author>;
</book>;
<!-- imagine more entries here... -->;
</catalog>;
#! /usr/bin/python
import xml.dom.minidom
from xml.dom.minidom import Node
doc = xml.dom.minidom.parse("books.xml")
mapping = {}
for node in doc.getElementsByTagName("book"):
isbn = node.getAttribute("isbn")
L = node.getElementsByTagName("title")
for node2 in L:
title = ""
for node3 in node2.childNodes:
if node3.nodeType == Node.TEXT_NODE:
title += node3.data
mapping[isbn] = title
# mapping now has the same value as in the SAX example:
print(mapping)
原文轉自:http://www.anti-gravitydesign.com