# structure, and convert it from and to XML.
##
-import string, sys, re
+import string, sys, re, platform
class _SimpleElementPath:
# emulate pre-1.2 find/findtext/findall behaviour
# @defreturn Element or None
def find(self, path):
+ if ElementPath.find(self, path) == None:
+ return ElementPath.find(self, path.encode())
return ElementPath.find(self, path)
##
# @defreturn string or None
def get(self, key, default=None):
- return self.attrib.get(key, default)
+ res = self.attrib.get(key, default)
+ if not res:
+ res = self.attrib.get(key.encode(), default)
+ if isinstance(res, bytes):
+ return res.decode()
+ else:
+ return res
##
# Sets an element attribute.
# @defreturn list of strings
def keys(self):
- return self.attrib.keys()
-
+ res = []
+ for key in self.attrib.keys():
+ if isinstance(key, bytes):
+ res.append(key.decode())
+ else:
+ res.append(key)
+ return res
+
##
# Gets element attributes, as a sequence. The attributes are
# returned in an arbitrary order.
def _escape_cdata(text, encoding=None, replace=str.replace):
# escape character data
try:
+ if platform.python_version()[0] == '2': # python 2.x.y
+ if encoding:
+ try:
+ text = _encode(text, encoding)
+ except UnicodeError:
+ return _encode_entity(text)
+
text = replace(text, "&", "&")
text = replace(text, "<", "<")
text = replace(text, ">", ">")
def _flush(self):
if self._data:
if self._last is not None:
- text = string.join(self._data, "")
+ text = ""
+ for item in self._data:
+ try:
+ text += item
+ except:
+ text += item.decode()
if self._tail:
assert self._last.tail is None, "internal error (tail)"
self._last.tail = text
parser.StartElementHandler = self._start_list
except AttributeError:
pass
- encoding = None
- if not parser.returns_unicode:
- encoding = "utf-8"
+ #encoding = None
+ #if not parser.returns_unicode:
+ # encoding = "utf-8"
# target.xml(encoding, None)
self._doctype = None
self.entity = {}