本文整理汇总了Python中xml.etree.ElementTree._raise_serialization_error方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree._raise_serialization_error方法的具体用法?Python ElementTree._raise_serialization_error怎么用?Python ElementTree._raise_serialization_error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.etree.ElementTree
的用法示例。
在下文中一共展示了ElementTree._raise_serialization_error方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _escape_cdata
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import _raise_serialization_error [as 别名]
def _escape_cdata(text, encoding):
if "<![CDATA[" in text:
try:
return text.encode(encoding, "xmlcharrefreplace")
except (TypeError, AttributeError):
ElementTree._raise_serialization_error(text)
else:
return original_escape_cdata(text, encoding)
示例2: _escape_attrib
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import _raise_serialization_error [as 别名]
def _escape_attrib(text, encoding=None, replace=string.replace):
# escape attribute value
try:
if encoding:
try:
text = ET._encode(text, encoding)
except UnicodeError:
return ET._encode_entity(text)
text = replace(text, "&", "&")
text = replace(text, "'", "'")
#text = replace(text, "\"", """)
text = replace(text, "<", "<")
text = replace(text, ">", ">")
return text
except (TypeError, AttributeError):
ET._raise_serialization_error(text)
示例3: _escape_cdata
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import _raise_serialization_error [as 别名]
def _escape_cdata(text, encoding):
# escape character data
try:
# it's worth avoiding do-nothing calls for strings that are
# shorter than 500 character, or so. assume that's, by far,
# the most common case in most applications.
if "&" in text:
text = text.replace("&", "&")
if "<" in text:
text = text.replace("<", "<")
if ">" in text:
text = text.replace(">", ">")
return text.encode(encoding, "xmlcharrefreplace")
except (TypeError, AttributeError):
ElementTree._raise_serialization_error(text)
#XXX this is the patch
except (UnicodeDecodeError):
return text.decode("utf-8").encode(encoding, "xmlcharrefreplace")
示例4: custom_xml_write
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import _raise_serialization_error [as 别名]
def custom_xml_write(self, file, node, encoding, namespaces, indentation='\n'):
"""
Custom write function based on ElementTree.ElementTree._write only for python 2.6
Basically it does the same but writes each attribute in a different line
The same was done with custom_serialize_xml for python 2.7
"""
tag = node.tag
next_indentation = node.tail
if tag is pyET.Comment:
file.write("<!-- %s -->" % pyET._escape_cdata(node.text, encoding))
elif tag is pyET.ProcessingInstruction:
file.write("<?%s?>" % pyET._escape_cdata(node.text, encoding))
else:
items = node.items()
xmlns_items = []
try:
if isinstance(tag, pyET.QName) or tag[:1] == "{":
tag, xmlns = pyET.fixtag(tag, namespaces)
if xmlns: xmlns_items.append(xmlns)
except TypeError:
pyET._raise_serialization_error(tag)
file.write("<" + pyET._encode(tag, encoding))
if items or xmlns_items:
items.sort()
for k, v in items:
try:
if isinstance(k, pyET.QName) or k[:1] == "{":
k, xmlns = pyET.fixtag(k, namespaces)
if xmlns: xmlns_items.append(xmlns)
except TypeError:
pyET._raise_serialization_error(k)
try:
if isinstance(v, pyET.QName):
v, xmlns = pyET.fixtag(v, namespaces)
if xmlns: xmlns_items.append(xmlns)
except TypeError:
pyET._raise_serialization_error(v)
file.write("%s\t\t%s=\"%s\"" % (indentation, pyET._encode(k, encoding),
pyET._escape_attrib(v, encoding)))
for k, v in xmlns_items:
file.write("%s\t\t%s=\"%s\"" % (indentation, pyET._encode(k, encoding),
pyET._escape_attrib(v, encoding)))
if node.text or len(node):
file.write(">")
if node.text:
file.write(pyET._escape_cdata(node.text, encoding))
for n in node:
self._write(file, n, encoding, namespaces, next_indentation)
file.write("</" + pyET._encode(tag, encoding) + ">")
else:
file.write(" />")
for k, v in xmlns_items:
del namespaces[v]
if node.tail:
file.write(pyET._escape_cdata(node.tail, encoding))
示例5: _write2
# 需要导入模块: from xml.etree import ElementTree [as 别名]
# 或者: from xml.etree.ElementTree import _raise_serialization_error [as 别名]
def _write2(self, file, node, encoding, namespaces):
# write XML to file
tag = node.tag
if tag is ElementTree.Comment:
text = node.text.encode(encoding)
file.write("<!--%s-->" % text)
elif tag is ElementTree.ProcessingInstruction:
text = node.text.encode(encoding)
file.write("<?%s?>" % text)
else:
items = node.items()
xmlns_items = [] # new namespaces in this scope
try:
if isinstance(tag, ElementTree.QName) or tag[:1] == "{":
tag, xmlns = ElementTree.fixtag(tag, namespaces)
if xmlns: xmlns_items.append(xmlns)
except TypeError:
ElementTree._raise_serialization_error(tag)
file.write("<" + ElementTree._encode(tag, encoding))
if items or xmlns_items:
items.sort() # lexical order
for k, v in items:
try:
if isinstance(k, ElementTree.QName) or k[:1] == "{":
k, xmlns = ElementTree.fixtag(k, namespaces)
if xmlns: xmlns_items.append(xmlns)
except TypeError:
ElementTree._raise_serialization_error(k)
try:
if isinstance(v, ElementTree.QName):
v, xmlns = ElementTree.fixtag(v, namespaces)
if xmlns: xmlns_items.append(xmlns)
except TypeError:
ElementTree._raise_serialization_error(v)
file.write(" %s=\"%s\"" % (ElementTree._encode(k, encoding),
ElementTree._escape_attrib(v, encoding)))
for k, v in xmlns_items:
file.write(" %s=\"%s\"" % (ElementTree._encode(k, encoding),
ElementTree._escape_attrib(v, encoding)))
if node.text or len(node):
file.write(">")
if node.text:
text = node.text.encode(encoding)
file.write(text)
for n in node:
self._write(file, n, encoding, namespaces)
file.write("</" + ElementTree._encode(tag, encoding) + ">")
else:
file.write(" />")
for k, v in xmlns_items:
del namespaces[v]
if node.tail:
tail = node.tail.encode(encoding)
file.write(tail)