1 # Very simple test - Parse a file and print what happens
3 # XXX TypeErrors on calling handlers, or on bad return values from a
4 # handler, are obscure and unhelpful.
6 from xml
.parsers
import expat
9 def StartElementHandler(self
, name
, attrs
):
10 print 'Start element:\n\t', repr(name
), attrs
12 def EndElementHandler(self
, name
):
13 print 'End element:\n\t', repr(name
)
15 def CharacterDataHandler(self
, data
):
18 print 'Character data:'
19 print '\t', repr(data
)
21 def ProcessingInstructionHandler(self
, target
, data
):
22 print 'PI:\n\t', repr(target
), repr(data
)
24 def StartNamespaceDeclHandler(self
, prefix
, uri
):
25 print 'NS decl:\n\t', repr(prefix
), repr(uri
)
27 def EndNamespaceDeclHandler(self
, prefix
):
28 print 'End of NS decl:\n\t', repr(prefix
)
30 def StartCdataSectionHandler(self
):
31 print 'Start of CDATA section'
33 def EndCdataSectionHandler(self
):
34 print 'End of CDATA section'
36 def CommentHandler(self
, text
):
37 print 'Comment:\n\t', repr(text
)
39 def NotationDeclHandler(self
, *args
):
40 name
, base
, sysid
, pubid
= args
41 print 'Notation declared:', args
43 def UnparsedEntityDeclHandler(self
, *args
):
44 entityName
, base
, systemId
, publicId
, notationName
= args
45 print 'Unparsed entity decl:\n\t', args
47 def NotStandaloneHandler(self
, userData
):
48 print 'Not standalone'
51 def ExternalEntityRefHandler(self
, *args
):
52 context
, base
, sysId
, pubId
= args
53 print 'External entity ref:', args
[1:]
56 def DefaultHandler(self
, userData
):
59 def DefaultHandlerExpand(self
, userData
):
70 parser
= expat
.ParserCreate(namespace_separator
='!')
72 # Test getting/setting returns_unicode
73 parser
.returns_unicode
= 0; confirm(parser
.returns_unicode
== 0)
74 parser
.returns_unicode
= 1; confirm(parser
.returns_unicode
== 1)
75 parser
.returns_unicode
= 2; confirm(parser
.returns_unicode
== 1)
76 parser
.returns_unicode
= 0; confirm(parser
.returns_unicode
== 0)
79 'StartElementHandler', 'EndElementHandler',
80 'CharacterDataHandler', 'ProcessingInstructionHandler',
81 'UnparsedEntityDeclHandler', 'NotationDeclHandler',
82 'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler',
83 'CommentHandler', 'StartCdataSectionHandler',
84 'EndCdataSectionHandler',
85 'DefaultHandler', 'DefaultHandlerExpand',
86 #'NotStandaloneHandler',
87 'ExternalEntityRefHandler'
89 for name
in HANDLER_NAMES
:
90 setattr(parser
, name
, getattr(out
, name
))
93 <?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
94 <?xml-stylesheet href="stylesheet.css"?>
96 <!DOCTYPE quotations SYSTEM "quotations.dtd" [
98 <!NOTATION notation SYSTEM "notation.jpeg">
99 <!ENTITY acirc "â">
100 <!ENTITY external_entity SYSTEM "entity.file">
101 <!ENTITY unparsed_entity SYSTEM "entity.file" NDATA notation>
105 <root attr1="value1" attr2="value2ὀ">
106 <myns:subelement xmlns:myns="http://www.python.org/namespace">
107 Contents of subelements
109 <sub2><![CDATA[contents of CDATA section]]></sub2>
114 # Produce UTF-8 output
115 parser
.returns_unicode
= 0
117 parser
.Parse(data
, 1)
119 print '** Error', parser
.ErrorCode
, expat
.ErrorString(parser
.ErrorCode
)
120 print '** Line', parser
.ErrorLineNumber
121 print '** Column', parser
.ErrorColumnNumber
122 print '** Byte', parser
.ErrorByteIndex
124 # Try the parse again, this time producing Unicode output
125 parser
= expat
.ParserCreate(namespace_separator
='!')
126 parser
.returns_unicode
= 1
128 for name
in HANDLER_NAMES
:
129 setattr(parser
, name
, getattr(out
, name
))
131 parser
.Parse(data
, 1)
133 print '** Error', parser
.ErrorCode
, expat
.ErrorString(parser
.ErrorCode
)
134 print '** Line', parser
.ErrorLineNumber
135 print '** Column', parser
.ErrorColumnNumber
136 print '** Byte', parser
.ErrorByteIndex
139 parser
= expat
.ParserCreate(namespace_separator
='!')
140 parser
.returns_unicode
= 1
142 for name
in HANDLER_NAMES
:
143 setattr(parser
, name
, getattr(out
, name
))
145 file = StringIO
.StringIO(data
)
147 parser
.ParseFile(file)
149 print '** Error', parser
.ErrorCode
, expat
.ErrorString(parser
.ErrorCode
)
150 print '** Line', parser
.ErrorLineNumber
151 print '** Column', parser
.ErrorColumnNumber
152 print '** Byte', parser
.ErrorByteIndex
155 # Tests that make sure we get errors when the namespace_separator value
156 # is illegal, and that we don't for good values:
158 print "Testing constructor for proper handling of namespace_separator values:"
160 expat
.ParserCreate(namespace_separator
=None)
161 expat
.ParserCreate(namespace_separator
=' ')
162 print "Legal values tested o.k."
164 expat
.ParserCreate(namespace_separator
=42)
166 print "Caught expected TypeError:"
169 print "Failed to catch expected TypeError."
171 expat
.ParserCreate(namespace_separator
='too long')
172 except ValueError, e
:
173 print "Caught expected ValueError:"
176 print "Failed to catch expected ValueError."
178 expat
.ParserCreate(namespace_separator
='') # too short
179 except ValueError, e
:
180 print "Caught expected ValueError:"
183 print "Failed to catch expected ValueError."