Guido van Rossum | aad6761 | 2000-05-08 17:31:04 +0000 | [diff] [blame] | 1 | # Very simple test - Parse a file and print what happens |
| 2 | |
| 3 | # XXX TypeErrors on calling handlers, or on bad return values from a |
| 4 | # handler, are obscure and unhelpful. |
| 5 | |
| 6 | import sys, string |
| 7 | import os |
| 8 | |
| 9 | import pyexpat |
| 10 | |
| 11 | class Outputter: |
| 12 | def StartElementHandler(self, name, attrs): |
| 13 | print 'Start element:\n\t', name, attrs |
| 14 | |
| 15 | def EndElementHandler(self, name): |
| 16 | print 'End element:\n\t', name |
| 17 | |
| 18 | def CharacterDataHandler(self, data): |
| 19 | data = string.strip(data) |
| 20 | if data: |
| 21 | print 'Character data:' |
| 22 | print '\t', repr(data) |
| 23 | |
| 24 | def ProcessingInstructionHandler(self, target, data): |
| 25 | print 'PI:\n\t', target, data |
| 26 | |
| 27 | def StartNamespaceDeclHandler(self, prefix, uri): |
| 28 | print 'NS decl:\n\t', prefix, uri |
| 29 | |
| 30 | def EndNamespaceDeclHandler(self, prefix): |
| 31 | print 'End of NS decl:\n\t', prefix |
| 32 | |
| 33 | def StartCdataSectionHandler(self): |
| 34 | print 'Start of CDATA section' |
| 35 | |
| 36 | def EndCdataSectionHandler(self): |
| 37 | print 'End of CDATA section' |
| 38 | |
| 39 | def CommentHandler(self, text): |
| 40 | print 'Comment:\n\t', repr(text) |
| 41 | |
| 42 | def NotationDeclHandler(self, *args): |
| 43 | name, base, sysid, pubid = args |
| 44 | print 'Notation declared:', args |
| 45 | |
| 46 | def UnparsedEntityDeclHandler(self, *args): |
| 47 | entityName, base, systemId, publicId, notationName = args |
| 48 | print 'Unparsed entity decl:\n\t', args |
| 49 | |
| 50 | def NotStandaloneHandler(self, userData): |
| 51 | print 'Not standalone' |
| 52 | return 1 |
| 53 | |
| 54 | def ExternalEntityRefHandler(self, context, base, sysId, pubId): |
| 55 | print 'External entity ref:', context, base, sysId, pubId |
| 56 | return 1 |
| 57 | |
| 58 | def DefaultHandler(self, userData): |
| 59 | pass |
| 60 | |
| 61 | def DefaultHandlerExpand(self, userData): |
| 62 | pass |
| 63 | |
| 64 | |
| 65 | out = Outputter() |
| 66 | parser = pyexpat.ParserCreate(namespace_separator='!') |
| 67 | for name in ['StartElementHandler', 'EndElementHandler', |
| 68 | 'CharacterDataHandler', 'ProcessingInstructionHandler', |
| 69 | 'UnparsedEntityDeclHandler', 'NotationDeclHandler', |
| 70 | 'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler', |
| 71 | 'CommentHandler', 'StartCdataSectionHandler', |
| 72 | 'EndCdataSectionHandler', |
| 73 | 'DefaultHandler', 'DefaultHandlerExpand', |
| 74 | #'NotStandaloneHandler', |
| 75 | 'ExternalEntityRefHandler' |
| 76 | ]: |
| 77 | setattr(parser, name, getattr(out, name) ) |
| 78 | |
| 79 | data = """<?xml version="1.0" encoding="iso-8859-1" standalone="no"?> |
| 80 | <?xml-stylesheet href="stylesheet.css"?> |
| 81 | <!-- comment data --> |
| 82 | <!DOCTYPE quotations SYSTEM "quotations.dtd" [ |
| 83 | <!ELEMENT root ANY> |
| 84 | <!NOTATION notation SYSTEM "notation.jpeg"> |
| 85 | <!ENTITY acirc "â"> |
| 86 | <!ENTITY external_entity SYSTEM "entity.file"> |
| 87 | <!ENTITY unparsed_entity SYSTEM "entity.file" NDATA notation> |
| 88 | %unparsed_entity; |
| 89 | ]> |
| 90 | |
| 91 | <root> |
| 92 | <myns:subelement xmlns:myns="http://www.python.org/namespace"> |
| 93 | Contents of subelements |
| 94 | </myns:subelement> |
| 95 | <sub2><![CDATA[contents of CDATA section]]></sub2> |
| 96 | &external_entity; |
| 97 | </root> |
| 98 | """ |
| 99 | |
| 100 | try: |
| 101 | parser.Parse(data, 1) |
| 102 | except pyexpat.error: |
| 103 | print '** Error', parser.ErrorCode, pyexpat.ErrorString( parser.ErrorCode) |
| 104 | print '** Line', parser.ErrorLineNumber |
| 105 | print '** Column', parser.ErrorColumnNumber |
| 106 | print '** Byte', parser.ErrorByteIndex |
| 107 | |