XML zu TXT konvertieren
Verfasst: Montag 16. Juni 2008, 15:10
Hallo,
ich bin absoluter Python-Anfänger brauche aber dringend Eure Hilfe.
Ich möchte Informationen aus einer XML-Datei herauslesen und in einer Textdatei abspeichern.
Unter:
http://de.wikibooks.org/wiki/Python_unter_Linux:_XML
habe ich folgende Skripte gefunden:
####
#!/usr/bin/python
# -*- coding: utf-8 -*-
import xml.dom.minidom
datei = open("testxml2.xml", "r")
dom = xml.dom.minidom.parse(datei)
datei.close()
def dokument(domina):
for node in domina.childNodes:
print "NodeName:", node.nodeName,
if node.nodeType == node.ELEMENT_NODE:
print "Typ ELEMENT_NODE"
elif node.nodeType == node.TEXT_NODE:
print "Typ TEXT_NODE, Content: ", node.nodeValue.strip()
elif node.nodeType == node.COMMENT_NODE:
print "Typ COMMENT_NODE, "
dokument(node)
dokument(dom)
####
bzw.
####
#!/usr/bin/python
# -*- coding: utf-8 -*-
import xml.sax
class MiniHandler(xml.sax.handler.ContentHandler):
def startDocument(self):
print "ANFANG"
def endDocument(self):
print "ENDE"
def startElement(self, name, attrs):
print "Element", name
def characters(self, content):
s = content.strip()
if s != "":
print "Textinhalt:", s
handler = MiniHandler()
datei = open("testxml2.xml", "r")
xml.sax.parse(datei, handler)
datei.close()
####
Diese funktionieren ganz gut.
Wie kann ich die Ausgabe in einer Datei speichern?
Mit “./python.py > datei.txt“ geht nicht!
Es muss programmiert werden.
Danke für Eure Hilfe!
Gruß
tibrandt
ich bin absoluter Python-Anfänger brauche aber dringend Eure Hilfe.
Ich möchte Informationen aus einer XML-Datei herauslesen und in einer Textdatei abspeichern.
Unter:
http://de.wikibooks.org/wiki/Python_unter_Linux:_XML
habe ich folgende Skripte gefunden:
####
#!/usr/bin/python
# -*- coding: utf-8 -*-
import xml.dom.minidom
datei = open("testxml2.xml", "r")
dom = xml.dom.minidom.parse(datei)
datei.close()
def dokument(domina):
for node in domina.childNodes:
print "NodeName:", node.nodeName,
if node.nodeType == node.ELEMENT_NODE:
print "Typ ELEMENT_NODE"
elif node.nodeType == node.TEXT_NODE:
print "Typ TEXT_NODE, Content: ", node.nodeValue.strip()
elif node.nodeType == node.COMMENT_NODE:
print "Typ COMMENT_NODE, "
dokument(node)
dokument(dom)
####
bzw.
####
#!/usr/bin/python
# -*- coding: utf-8 -*-
import xml.sax
class MiniHandler(xml.sax.handler.ContentHandler):
def startDocument(self):
print "ANFANG"
def endDocument(self):
print "ENDE"
def startElement(self, name, attrs):
print "Element", name
def characters(self, content):
s = content.strip()
if s != "":
print "Textinhalt:", s
handler = MiniHandler()
datei = open("testxml2.xml", "r")
xml.sax.parse(datei, handler)
datei.close()
####
Diese funktionieren ganz gut.
Wie kann ich die Ausgabe in einer Datei speichern?
Mit “./python.py > datei.txt“ geht nicht!
Es muss programmiert werden.
Danke für Eure Hilfe!
Gruß
tibrandt