Python - čtení XML
Ješte jeden tip, který se může hodit. Čtení XML v Pythonu pomocí DOM.
#!/usr/bin/python
import sys
import pprint
import xml.dom.minidom
from xml.dom.minidom import Node
doc = xml.dom.minidom.parse("book.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;
pprint.pprint(mapping)
sys.exit(0)Zdrojove XML:
<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>
</catalog>
vytvořil BlackShark 26.3. 2008 11:23, naposledy změněnil BlackShark 26.3. 2008 11:25
přečteno: 879x