versuche mich gerade an xml and python und bemühe dazu das hier anscheinend [ironie]recht bliebte [/ironie] buch von galileo.
Also das ganze sieht so aus:
Code: Alles auswählen
import xml.dom.minidom as dom
def _erstelle_eintrag(schluessel, wert):
tag_eintrag = dom.Element("eintrag")
tag_schluessel = dom.Element("schluessel")
tag_wert = dom.Element("wert")
tag_schluessel.setAttribute("typ", type(schluessel).__name__)
tag_wert.setAttribute("typ", type(wert).__name__)
text = dom.Text()
text.data = str(schluessel)
tag_schluessel.appendChild(text)
text = dom.Text()
text.data = str(wert)
tag_wert.appendChild(text)
tag_eintrag.appendChild(tag_schluessel)
tag_eintrag.appendChild(tag_wert)
return tag_eintrag
def schreibe_dict(d, dateiname):
baum = dom.Document()
tag_dict = dom.Element("dictionary")
baum.appendChild(tag_dict)
for schluessel, wert in d.iteritems():
tag_eintrag = _erstelle_eintrag(schluessel, wert)
tag_dict.appendChild(tag_eintrag)
f = open(dateiname, "w")
baum.writexml(f, "", "\t", "\n")
f.close()
Code: Alles auswählen
woerterbuch = {"Germany" : "Deutschland", "Spain" : "Spanien"}
chreibe_dict(woerterbuch, "test_dict_to_xml.xml")
Traceback (most recent call last):
File "<pyshell#226>", line 1, in <module>
schreibe_dict(woerterbuch, "test_dict_to_xml.xml")
File "C:/Python25/create_xml_doc.py", line 30, in schreibe_dict
tag_dict.appendChild(tag_eintrag)
File "C:\Python25\lib\xml\dom\minidom.py", line 121, in appendChild
_clear_id_cache(self)
File "C:\Python25\lib\xml\dom\minidom.py", line 1466, in _clear_id_cache
node.ownerDocument._id_cache.clear()
AttributeError: 'NoneType' object has no attribute '_id_cache'
Die frage ist jetzt, wo ist der Fehler da in dem Beispiel?