22 STATE_IDLE, STATE_CHARACTER_DATA, STATE_START_TAG = xrange(3)
27 if not isinstance(name, unicode):
28 raise TypeError,
"invalid argument type (must be unicode)"
36 (c >=
'0' and c <=
'9')
or \
38 (c >= 0x0300
and c <= 0x036f)
or \
39 (c >= 0x203f
and c <= 0x2040):
43 if c !=
'-' and c !=
'.' and \
44 not (c >=
'0' and c <=
'9')
and c !=
':' and \
45 not (c >=
'A' and c <=
'Z')
and c !=
'_' and \
46 not (c >=
'a' and c <=
'z')
and c != 0xb7
and \
47 not (c >=
u'\u00c0' and c <=
u'\u00d6')
and \
48 not (c >=
u'\u00d8' and c <=
u'\u00f6')
and \
49 not (c >=
u'\u00f8' and c <=
u'\u02ff')
and \
50 not (c >=
u'\u0300' and c <=
u'\u036f')
and \
51 not (c >=
u'\u0370' and c <=
u'\u037d')
and \
52 not (c >=
u'\u037f' and c <=
u'\u1fff')
and \
53 not (c >=
u'\u200c' and c <=
u'\u200d')
and \
54 not (c >=
u'\u203f' and c <=
u'\u2040')
and \
55 not (c >=
u'\u2070' and c <=
u'\u218f')
and \
56 not (c >=
u'\u2c00' and c <=
u'\u2fef')
and \
57 not (c >=
u'\u3001' and c <=
u'\ud7ff')
and \
58 not (c >=
u'\uf900' and c <=
u'\ufdcf')
and \
59 not (c >=
u'\ufdf0' and c <=
u'\ufffd'):
68 if not isinstance(data, unicode):
69 raise TypeError,
"invalid argument type (must be unicode)"
71 return data.replace(
'&',
'&') \
72 .replace(
'<',
'<') \
73 .replace(
'>',
'>') \
74 .replace(
'"',
'"')
126 raise NotImplementedError
128 def _prepare_for_data(self):
130 self.
write(
'<?xml version="1.0" encoding="UTF-8"?>\n')
133 if self.
state == STATE_START_TAG:
137 self.current_attrs.clear()
138 self.
state = STATE_IDLE
149 raise ValueError,
"invalid element name '%s'" % name
153 raise ValueError,
"only one root element allowed"
158 if self.
state == STATE_CHARACTER_DATA:
161 self.
write(
'<' + name)
164 self.stack.append(name)
165 self.
state = STATE_START_TAG
170 if self.
state == STATE_START_TAG:
174 if self.
state == STATE_CHARACTER_DATA:
179 raise ValueError,
"can't end element at root level"
181 self.
state = STATE_IDLE
188 self.current_attrs.clear()
201 value = unicode(value)
202 if self.
state != STATE_START_TAG:
203 raise ValueError,
"can't write attributes right now"
205 raise ValueError,
"invalid attribute name '%s'" % name
207 raise ValueError,
"line feed character in attribute value"
211 raise ValueError,
"duplicate attribute name '%s'" % name
212 self.current_attrs.add(name)
226 "can't write character data outside of root element"
231 self.
state = STATE_CHARACTER_DATA
245 "can't write CDATA section outside of root element"
250 self.
state = STATE_CHARACTER_DATA
252 self.
write(
'<![CDATA[')
253 self.
write(data.replace(
']]>',
']]>]]><![CDATA['))
def escape
Escape XML metacharacters in a string.
def write_cdata_section
Write a CDATA section.
def write_character_data
Write character data.
def end_element
Write a closing tag for the innermost element.
def start_element
Write an opening tag for a new element.
def valid_name
Checks whether a string is a valid XML name.
def write_attribute
Write an attribute for the innermost element.
def __init__
Create an XMLWriter instance.
def is_done
Return whether the document written so far is complete.