2 A simple demo that reads in an XML document and spits out an equivalent,
3 but not necessarily identical, document.
8 from xml
.sax
import saxutils
, handler
, make_parser
10 # --- The ContentHandler
12 class ContentGenerator(handler
.ContentHandler
):
14 def __init__(self
, out
=sys
.stdout
):
15 handler
.ContentHandler
.__init
__(self
)
18 # ContentHandler methods
20 def startDocument(self
):
21 self
._out
.write('<?xml version="1.0" encoding="iso-8859-1"?>\n')
23 def startElement(self
, name
, attrs
):
24 self
._out
.write('<' + name
)
25 for (name
, value
) in attrs
.items():
26 self
._out
.write(' %s="%s"' % (name
, saxutils
.escape(value
)))
29 def endElement(self
, name
):
30 self
._out
.write('</%s>' % name
)
32 def characters(self
, content
):
33 self
._out
.write(saxutils
.escape(content
))
35 def ignorableWhitespace(self
, content
):
36 self
._out
.write(content
)
38 def processingInstruction(self
, target
, data
):
39 self
._out
.write('<?%s %s?>' % (target
, data
))
41 # --- The main program
43 if __name__
== '__main__':
44 parser
= make_parser()
45 parser
.setContentHandler(ContentGenerator())
46 parser
.parse(sys
.argv
[1])