Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
<xml>
<Knoten>
<tag1>... info ...</tag1>
<tag2>... info ...</tag2>
<tag3>... info ...</tag3>
<tag4>... info ...</tag4>
</Knoten>
</xml>
Ich arbeite mit dem DOM Parser. Irgendwie muss ich hier immer den Namen der Tags ( .getElementsByTagName )angeben, um die Werte zu bestimmen. Leider verändern sich die Tags in meinen Input XML Files.
import xml.dom.minidom
s = """<xml>
<Knoten>
<tag1>... info ...</tag1>
<tag2>... info ...</tag2>
<tag3>... info ...</tag3>
<tag4>... info ...</tag4>
</Knoten>
</xml>"""
dom = xml.dom.minidom.parseString(s)
print [dom.getElementsByTagName('tag' + str(num))[0] for num in range(1,5)]
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
import xml.dom.minidom
s = """<xml>
<Knoten>
<tag1>... info ...</tag1>
<tag2>... info ...</tag2>
<tag3>... info ...</tag3>
<tag4>... info ...</tag4>
</Knoten>
</xml>"""
dom = xml.dom.minidom.parseString(s)
print [dom.getElementsByTagName('tag' + str(num))[0] for num in range(1,5)]
keboo hat geschrieben:Gibts da nicht irgendwie auch eine Lösung mit DOM?
Wo ist das Problem mit Minidom? Minidom ist auch eine DOM-Implementation.
Minidom-Duku hat geschrieben:xml.dom.minidom is a light-weight implementation of the Document Object Model interface. It is intended to be simpler than the full DOM and also significantly smaller.
Aber davon abgesehen ist ElementTree/lxml einfach pythonischer, der Code sieht wesentlich besser aus.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice