Last change
on this file since 3 was
3,
checked in by bastiaans, 19 years ago
|
daemon/xmlparse.py:
Initial import of xml parsing
|
File size:
1.0 KB
|
Rev | Line | |
---|
[3] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | import xml.sax |
---|
| 4 | |
---|
| 5 | class ContentGenerator( xml.sax.handler.ContentHandler ): |
---|
| 6 | |
---|
| 7 | def __init__(self, out = sys.stdout): |
---|
| 8 | handler.ContentHandler.__init__(self) |
---|
| 9 | self._out = out |
---|
| 10 | |
---|
| 11 | # ContentHandler methods |
---|
| 12 | |
---|
| 13 | def startDocument(self): |
---|
| 14 | self._out.write('<?xml version="1.0" encoding="iso-8859-1"?>\n') |
---|
| 15 | |
---|
| 16 | def startElement(self, name, attrs): |
---|
| 17 | self._out.write('<' + name) |
---|
| 18 | for (name, value) in attrs.items(): |
---|
| 19 | self._out.write(' %s="%s"' % (name, saxutils.escape(value))) |
---|
| 20 | self._out.write('>') |
---|
| 21 | |
---|
| 22 | def endElement(self, name): |
---|
| 23 | self._out.write('</%s>' % name) |
---|
| 24 | |
---|
| 25 | def characters(self, content): |
---|
| 26 | self._out.write(saxutils.escape(content)) |
---|
| 27 | |
---|
| 28 | def ignorableWhitespace(self, content): |
---|
| 29 | self._out.write(content) |
---|
| 30 | |
---|
| 31 | def processingInstruction(self, target, data): |
---|
| 32 | self._out.write('<?%s %s?>' % (target, data)) |
---|
| 33 | |
---|
| 34 | # --- The main program |
---|
| 35 | |
---|
| 36 | parser = xml.sax.make_parser() |
---|
| 37 | parser.setContentHandler(ContentGenerator()) |
---|
| 38 | parser.parse(sys.argv[1]) |
---|
Note: See
TracBrowser
for help on using the repository browser.