Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python2..../xml/etree
File: ElementTree.py
if elem.tail:
[1000] Fix | Delete
write(_escape_cdata(elem.tail, encoding))
[1001] Fix | Delete
[1002] Fix | Delete
def _serialize_text(write, elem, encoding):
[1003] Fix | Delete
for part in elem.itertext():
[1004] Fix | Delete
write(part.encode(encoding))
[1005] Fix | Delete
if elem.tail:
[1006] Fix | Delete
write(elem.tail.encode(encoding))
[1007] Fix | Delete
[1008] Fix | Delete
_serialize = {
[1009] Fix | Delete
"xml": _serialize_xml,
[1010] Fix | Delete
"html": _serialize_html,
[1011] Fix | Delete
"text": _serialize_text,
[1012] Fix | Delete
# this optional method is imported at the end of the module
[1013] Fix | Delete
# "c14n": _serialize_c14n,
[1014] Fix | Delete
}
[1015] Fix | Delete
[1016] Fix | Delete
##
[1017] Fix | Delete
# Registers a namespace prefix. The registry is global, and any
[1018] Fix | Delete
# existing mapping for either the given prefix or the namespace URI
[1019] Fix | Delete
# will be removed.
[1020] Fix | Delete
#
[1021] Fix | Delete
# @param prefix Namespace prefix.
[1022] Fix | Delete
# @param uri Namespace uri. Tags and attributes in this namespace
[1023] Fix | Delete
# will be serialized with the given prefix, if at all possible.
[1024] Fix | Delete
# @exception ValueError If the prefix is reserved, or is otherwise
[1025] Fix | Delete
# invalid.
[1026] Fix | Delete
[1027] Fix | Delete
def register_namespace(prefix, uri):
[1028] Fix | Delete
if re.match("ns\d+$", prefix):
[1029] Fix | Delete
raise ValueError("Prefix format reserved for internal use")
[1030] Fix | Delete
for k, v in _namespace_map.items():
[1031] Fix | Delete
if k == uri or v == prefix:
[1032] Fix | Delete
del _namespace_map[k]
[1033] Fix | Delete
_namespace_map[uri] = prefix
[1034] Fix | Delete
[1035] Fix | Delete
_namespace_map = {
[1036] Fix | Delete
# "well-known" namespace prefixes
[1037] Fix | Delete
"http://www.w3.org/XML/1998/namespace": "xml",
[1038] Fix | Delete
"http://www.w3.org/1999/xhtml": "html",
[1039] Fix | Delete
"http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf",
[1040] Fix | Delete
"http://schemas.xmlsoap.org/wsdl/": "wsdl",
[1041] Fix | Delete
# xml schema
[1042] Fix | Delete
"http://www.w3.org/2001/XMLSchema": "xs",
[1043] Fix | Delete
"http://www.w3.org/2001/XMLSchema-instance": "xsi",
[1044] Fix | Delete
# dublin core
[1045] Fix | Delete
"http://purl.org/dc/elements/1.1/": "dc",
[1046] Fix | Delete
}
[1047] Fix | Delete
[1048] Fix | Delete
def _raise_serialization_error(text):
[1049] Fix | Delete
raise TypeError(
[1050] Fix | Delete
"cannot serialize %r (type %s)" % (text, type(text).__name__)
[1051] Fix | Delete
)
[1052] Fix | Delete
[1053] Fix | Delete
def _encode(text, encoding):
[1054] Fix | Delete
try:
[1055] Fix | Delete
return text.encode(encoding, "xmlcharrefreplace")
[1056] Fix | Delete
except (TypeError, AttributeError):
[1057] Fix | Delete
_raise_serialization_error(text)
[1058] Fix | Delete
[1059] Fix | Delete
def _escape_cdata(text, encoding):
[1060] Fix | Delete
# escape character data
[1061] Fix | Delete
try:
[1062] Fix | Delete
# it's worth avoiding do-nothing calls for strings that are
[1063] Fix | Delete
# shorter than 500 character, or so. assume that's, by far,
[1064] Fix | Delete
# the most common case in most applications.
[1065] Fix | Delete
if "&" in text:
[1066] Fix | Delete
text = text.replace("&", "&")
[1067] Fix | Delete
if "<" in text:
[1068] Fix | Delete
text = text.replace("<", "&lt;")
[1069] Fix | Delete
if ">" in text:
[1070] Fix | Delete
text = text.replace(">", "&gt;")
[1071] Fix | Delete
return text.encode(encoding, "xmlcharrefreplace")
[1072] Fix | Delete
except (TypeError, AttributeError):
[1073] Fix | Delete
_raise_serialization_error(text)
[1074] Fix | Delete
[1075] Fix | Delete
def _escape_attrib(text, encoding):
[1076] Fix | Delete
# escape attribute value
[1077] Fix | Delete
try:
[1078] Fix | Delete
if "&" in text:
[1079] Fix | Delete
text = text.replace("&", "&amp;")
[1080] Fix | Delete
if "<" in text:
[1081] Fix | Delete
text = text.replace("<", "&lt;")
[1082] Fix | Delete
if ">" in text:
[1083] Fix | Delete
text = text.replace(">", "&gt;")
[1084] Fix | Delete
if "\"" in text:
[1085] Fix | Delete
text = text.replace("\"", "&quot;")
[1086] Fix | Delete
if "\n" in text:
[1087] Fix | Delete
text = text.replace("\n", "&#10;")
[1088] Fix | Delete
return text.encode(encoding, "xmlcharrefreplace")
[1089] Fix | Delete
except (TypeError, AttributeError):
[1090] Fix | Delete
_raise_serialization_error(text)
[1091] Fix | Delete
[1092] Fix | Delete
def _escape_attrib_html(text, encoding):
[1093] Fix | Delete
# escape attribute value
[1094] Fix | Delete
try:
[1095] Fix | Delete
if "&" in text:
[1096] Fix | Delete
text = text.replace("&", "&amp;")
[1097] Fix | Delete
if ">" in text:
[1098] Fix | Delete
text = text.replace(">", "&gt;")
[1099] Fix | Delete
if "\"" in text:
[1100] Fix | Delete
text = text.replace("\"", "&quot;")
[1101] Fix | Delete
return text.encode(encoding, "xmlcharrefreplace")
[1102] Fix | Delete
except (TypeError, AttributeError):
[1103] Fix | Delete
_raise_serialization_error(text)
[1104] Fix | Delete
[1105] Fix | Delete
# --------------------------------------------------------------------
[1106] Fix | Delete
[1107] Fix | Delete
##
[1108] Fix | Delete
# Generates a string representation of an XML element, including all
[1109] Fix | Delete
# subelements.
[1110] Fix | Delete
#
[1111] Fix | Delete
# @param element An Element instance.
[1112] Fix | Delete
# @keyparam encoding Optional output encoding (default is US-ASCII).
[1113] Fix | Delete
# @keyparam method Optional output method ("xml", "html", "text" or
[1114] Fix | Delete
# "c14n"; default is "xml").
[1115] Fix | Delete
# @return An encoded string containing the XML data.
[1116] Fix | Delete
# @defreturn string
[1117] Fix | Delete
[1118] Fix | Delete
def tostring(element, encoding=None, method=None):
[1119] Fix | Delete
class dummy:
[1120] Fix | Delete
pass
[1121] Fix | Delete
data = []
[1122] Fix | Delete
file = dummy()
[1123] Fix | Delete
file.write = data.append
[1124] Fix | Delete
ElementTree(element).write(file, encoding, method=method)
[1125] Fix | Delete
return "".join(data)
[1126] Fix | Delete
[1127] Fix | Delete
##
[1128] Fix | Delete
# Generates a string representation of an XML element, including all
[1129] Fix | Delete
# subelements. The string is returned as a sequence of string fragments.
[1130] Fix | Delete
#
[1131] Fix | Delete
# @param element An Element instance.
[1132] Fix | Delete
# @keyparam encoding Optional output encoding (default is US-ASCII).
[1133] Fix | Delete
# @keyparam method Optional output method ("xml", "html", "text" or
[1134] Fix | Delete
# "c14n"; default is "xml").
[1135] Fix | Delete
# @return A sequence object containing the XML data.
[1136] Fix | Delete
# @defreturn sequence
[1137] Fix | Delete
# @since 1.3
[1138] Fix | Delete
[1139] Fix | Delete
def tostringlist(element, encoding=None, method=None):
[1140] Fix | Delete
class dummy:
[1141] Fix | Delete
pass
[1142] Fix | Delete
data = []
[1143] Fix | Delete
file = dummy()
[1144] Fix | Delete
file.write = data.append
[1145] Fix | Delete
ElementTree(element).write(file, encoding, method=method)
[1146] Fix | Delete
# FIXME: merge small fragments into larger parts
[1147] Fix | Delete
return data
[1148] Fix | Delete
[1149] Fix | Delete
##
[1150] Fix | Delete
# Writes an element tree or element structure to sys.stdout. This
[1151] Fix | Delete
# function should be used for debugging only.
[1152] Fix | Delete
# <p>
[1153] Fix | Delete
# The exact output format is implementation dependent. In this
[1154] Fix | Delete
# version, it's written as an ordinary XML file.
[1155] Fix | Delete
#
[1156] Fix | Delete
# @param elem An element tree or an individual element.
[1157] Fix | Delete
[1158] Fix | Delete
def dump(elem):
[1159] Fix | Delete
# debugging
[1160] Fix | Delete
if not isinstance(elem, ElementTree):
[1161] Fix | Delete
elem = ElementTree(elem)
[1162] Fix | Delete
elem.write(sys.stdout)
[1163] Fix | Delete
tail = elem.getroot().tail
[1164] Fix | Delete
if not tail or tail[-1] != "\n":
[1165] Fix | Delete
sys.stdout.write("\n")
[1166] Fix | Delete
[1167] Fix | Delete
# --------------------------------------------------------------------
[1168] Fix | Delete
# parsing
[1169] Fix | Delete
[1170] Fix | Delete
##
[1171] Fix | Delete
# Parses an XML document into an element tree.
[1172] Fix | Delete
#
[1173] Fix | Delete
# @param source A filename or file object containing XML data.
[1174] Fix | Delete
# @param parser An optional parser instance. If not given, the
[1175] Fix | Delete
# standard {@link XMLParser} parser is used.
[1176] Fix | Delete
# @return An ElementTree instance
[1177] Fix | Delete
[1178] Fix | Delete
def parse(source, parser=None):
[1179] Fix | Delete
tree = ElementTree()
[1180] Fix | Delete
tree.parse(source, parser)
[1181] Fix | Delete
return tree
[1182] Fix | Delete
[1183] Fix | Delete
##
[1184] Fix | Delete
# Parses an XML document into an element tree incrementally, and reports
[1185] Fix | Delete
# what's going on to the user.
[1186] Fix | Delete
#
[1187] Fix | Delete
# @param source A filename or file object containing XML data.
[1188] Fix | Delete
# @param events A list of events to report back. If omitted, only "end"
[1189] Fix | Delete
# events are reported.
[1190] Fix | Delete
# @param parser An optional parser instance. If not given, the
[1191] Fix | Delete
# standard {@link XMLParser} parser is used.
[1192] Fix | Delete
# @return A (event, elem) iterator.
[1193] Fix | Delete
[1194] Fix | Delete
def iterparse(source, events=None, parser=None):
[1195] Fix | Delete
close_source = False
[1196] Fix | Delete
if not hasattr(source, "read"):
[1197] Fix | Delete
source = open(source, "rb")
[1198] Fix | Delete
close_source = True
[1199] Fix | Delete
try:
[1200] Fix | Delete
if not parser:
[1201] Fix | Delete
parser = XMLParser(target=TreeBuilder())
[1202] Fix | Delete
return _IterParseIterator(source, events, parser, close_source)
[1203] Fix | Delete
except:
[1204] Fix | Delete
if close_source:
[1205] Fix | Delete
source.close()
[1206] Fix | Delete
raise
[1207] Fix | Delete
[1208] Fix | Delete
class _IterParseIterator(object):
[1209] Fix | Delete
[1210] Fix | Delete
def __init__(self, source, events, parser, close_source=False):
[1211] Fix | Delete
self._file = source
[1212] Fix | Delete
self._close_file = close_source
[1213] Fix | Delete
self._events = []
[1214] Fix | Delete
self._index = 0
[1215] Fix | Delete
self._error = None
[1216] Fix | Delete
self.root = self._root = None
[1217] Fix | Delete
self._parser = parser
[1218] Fix | Delete
# wire up the parser for event reporting
[1219] Fix | Delete
parser = self._parser._parser
[1220] Fix | Delete
append = self._events.append
[1221] Fix | Delete
if events is None:
[1222] Fix | Delete
events = ["end"]
[1223] Fix | Delete
for event in events:
[1224] Fix | Delete
if event == "start":
[1225] Fix | Delete
try:
[1226] Fix | Delete
parser.ordered_attributes = 1
[1227] Fix | Delete
parser.specified_attributes = 1
[1228] Fix | Delete
def handler(tag, attrib_in, event=event, append=append,
[1229] Fix | Delete
start=self._parser._start_list):
[1230] Fix | Delete
append((event, start(tag, attrib_in)))
[1231] Fix | Delete
parser.StartElementHandler = handler
[1232] Fix | Delete
except AttributeError:
[1233] Fix | Delete
def handler(tag, attrib_in, event=event, append=append,
[1234] Fix | Delete
start=self._parser._start):
[1235] Fix | Delete
append((event, start(tag, attrib_in)))
[1236] Fix | Delete
parser.StartElementHandler = handler
[1237] Fix | Delete
elif event == "end":
[1238] Fix | Delete
def handler(tag, event=event, append=append,
[1239] Fix | Delete
end=self._parser._end):
[1240] Fix | Delete
append((event, end(tag)))
[1241] Fix | Delete
parser.EndElementHandler = handler
[1242] Fix | Delete
elif event == "start-ns":
[1243] Fix | Delete
def handler(prefix, uri, event=event, append=append):
[1244] Fix | Delete
try:
[1245] Fix | Delete
uri = (uri or "").encode("ascii")
[1246] Fix | Delete
except UnicodeError:
[1247] Fix | Delete
pass
[1248] Fix | Delete
append((event, (prefix or "", uri or "")))
[1249] Fix | Delete
parser.StartNamespaceDeclHandler = handler
[1250] Fix | Delete
elif event == "end-ns":
[1251] Fix | Delete
def handler(prefix, event=event, append=append):
[1252] Fix | Delete
append((event, None))
[1253] Fix | Delete
parser.EndNamespaceDeclHandler = handler
[1254] Fix | Delete
else:
[1255] Fix | Delete
raise ValueError("unknown event %r" % event)
[1256] Fix | Delete
[1257] Fix | Delete
def next(self):
[1258] Fix | Delete
try:
[1259] Fix | Delete
while 1:
[1260] Fix | Delete
try:
[1261] Fix | Delete
item = self._events[self._index]
[1262] Fix | Delete
self._index += 1
[1263] Fix | Delete
return item
[1264] Fix | Delete
except IndexError:
[1265] Fix | Delete
pass
[1266] Fix | Delete
if self._error:
[1267] Fix | Delete
e = self._error
[1268] Fix | Delete
self._error = None
[1269] Fix | Delete
raise e
[1270] Fix | Delete
if self._parser is None:
[1271] Fix | Delete
self.root = self._root
[1272] Fix | Delete
break
[1273] Fix | Delete
# load event buffer
[1274] Fix | Delete
del self._events[:]
[1275] Fix | Delete
self._index = 0
[1276] Fix | Delete
data = self._file.read(16384)
[1277] Fix | Delete
if data:
[1278] Fix | Delete
try:
[1279] Fix | Delete
self._parser.feed(data)
[1280] Fix | Delete
except SyntaxError as exc:
[1281] Fix | Delete
self._error = exc
[1282] Fix | Delete
else:
[1283] Fix | Delete
self._root = self._parser.close()
[1284] Fix | Delete
self._parser = None
[1285] Fix | Delete
except:
[1286] Fix | Delete
if self._close_file:
[1287] Fix | Delete
self._file.close()
[1288] Fix | Delete
raise
[1289] Fix | Delete
if self._close_file:
[1290] Fix | Delete
self._file.close()
[1291] Fix | Delete
raise StopIteration
[1292] Fix | Delete
[1293] Fix | Delete
def __iter__(self):
[1294] Fix | Delete
return self
[1295] Fix | Delete
[1296] Fix | Delete
##
[1297] Fix | Delete
# Parses an XML document from a string constant. This function can
[1298] Fix | Delete
# be used to embed "XML literals" in Python code.
[1299] Fix | Delete
#
[1300] Fix | Delete
# @param source A string containing XML data.
[1301] Fix | Delete
# @param parser An optional parser instance. If not given, the
[1302] Fix | Delete
# standard {@link XMLParser} parser is used.
[1303] Fix | Delete
# @return An Element instance.
[1304] Fix | Delete
# @defreturn Element
[1305] Fix | Delete
[1306] Fix | Delete
def XML(text, parser=None):
[1307] Fix | Delete
if not parser:
[1308] Fix | Delete
parser = XMLParser(target=TreeBuilder())
[1309] Fix | Delete
parser.feed(text)
[1310] Fix | Delete
return parser.close()
[1311] Fix | Delete
[1312] Fix | Delete
##
[1313] Fix | Delete
# Parses an XML document from a string constant, and also returns
[1314] Fix | Delete
# a dictionary which maps from element id:s to elements.
[1315] Fix | Delete
#
[1316] Fix | Delete
# @param source A string containing XML data.
[1317] Fix | Delete
# @param parser An optional parser instance. If not given, the
[1318] Fix | Delete
# standard {@link XMLParser} parser is used.
[1319] Fix | Delete
# @return A tuple containing an Element instance and a dictionary.
[1320] Fix | Delete
# @defreturn (Element, dictionary)
[1321] Fix | Delete
[1322] Fix | Delete
def XMLID(text, parser=None):
[1323] Fix | Delete
if not parser:
[1324] Fix | Delete
parser = XMLParser(target=TreeBuilder())
[1325] Fix | Delete
parser.feed(text)
[1326] Fix | Delete
tree = parser.close()
[1327] Fix | Delete
ids = {}
[1328] Fix | Delete
for elem in tree.iter():
[1329] Fix | Delete
id = elem.get("id")
[1330] Fix | Delete
if id:
[1331] Fix | Delete
ids[id] = elem
[1332] Fix | Delete
return tree, ids
[1333] Fix | Delete
[1334] Fix | Delete
##
[1335] Fix | Delete
# Parses an XML document from a string constant. Same as {@link #XML}.
[1336] Fix | Delete
#
[1337] Fix | Delete
# @def fromstring(text)
[1338] Fix | Delete
# @param source A string containing XML data.
[1339] Fix | Delete
# @return An Element instance.
[1340] Fix | Delete
# @defreturn Element
[1341] Fix | Delete
[1342] Fix | Delete
fromstring = XML
[1343] Fix | Delete
[1344] Fix | Delete
##
[1345] Fix | Delete
# Parses an XML document from a sequence of string fragments.
[1346] Fix | Delete
#
[1347] Fix | Delete
# @param sequence A list or other sequence containing XML data fragments.
[1348] Fix | Delete
# @param parser An optional parser instance. If not given, the
[1349] Fix | Delete
# standard {@link XMLParser} parser is used.
[1350] Fix | Delete
# @return An Element instance.
[1351] Fix | Delete
# @defreturn Element
[1352] Fix | Delete
# @since 1.3
[1353] Fix | Delete
[1354] Fix | Delete
def fromstringlist(sequence, parser=None):
[1355] Fix | Delete
if not parser:
[1356] Fix | Delete
parser = XMLParser(target=TreeBuilder())
[1357] Fix | Delete
for text in sequence:
[1358] Fix | Delete
parser.feed(text)
[1359] Fix | Delete
return parser.close()
[1360] Fix | Delete
[1361] Fix | Delete
# --------------------------------------------------------------------
[1362] Fix | Delete
[1363] Fix | Delete
##
[1364] Fix | Delete
# Generic element structure builder. This builder converts a sequence
[1365] Fix | Delete
# of {@link #TreeBuilder.start}, {@link #TreeBuilder.data}, and {@link
[1366] Fix | Delete
# #TreeBuilder.end} method calls to a well-formed element structure.
[1367] Fix | Delete
# <p>
[1368] Fix | Delete
# You can use this class to build an element structure using a custom XML
[1369] Fix | Delete
# parser, or a parser for some other XML-like format.
[1370] Fix | Delete
#
[1371] Fix | Delete
# @param element_factory Optional element factory. This factory
[1372] Fix | Delete
# is called to create new Element instances, as necessary.
[1373] Fix | Delete
[1374] Fix | Delete
class TreeBuilder(object):
[1375] Fix | Delete
[1376] Fix | Delete
def __init__(self, element_factory=None):
[1377] Fix | Delete
self._data = [] # data collector
[1378] Fix | Delete
self._elem = [] # element stack
[1379] Fix | Delete
self._last = None # last element
[1380] Fix | Delete
self._tail = None # true if we're after an end tag
[1381] Fix | Delete
if element_factory is None:
[1382] Fix | Delete
element_factory = Element
[1383] Fix | Delete
self._factory = element_factory
[1384] Fix | Delete
[1385] Fix | Delete
##
[1386] Fix | Delete
# Flushes the builder buffers, and returns the toplevel document
[1387] Fix | Delete
# element.
[1388] Fix | Delete
#
[1389] Fix | Delete
# @return An Element instance.
[1390] Fix | Delete
# @defreturn Element
[1391] Fix | Delete
[1392] Fix | Delete
def close(self):
[1393] Fix | Delete
assert len(self._elem) == 0, "missing end tags"
[1394] Fix | Delete
assert self._last is not None, "missing toplevel element"
[1395] Fix | Delete
return self._last
[1396] Fix | Delete
[1397] Fix | Delete
def _flush(self):
[1398] Fix | Delete
if self._data:
[1399] Fix | Delete
if self._last is not None:
[1400] Fix | Delete
text = "".join(self._data)
[1401] Fix | Delete
if self._tail:
[1402] Fix | Delete
assert self._last.tail is None, "internal error (tail)"
[1403] Fix | Delete
self._last.tail = text
[1404] Fix | Delete
else:
[1405] Fix | Delete
assert self._last.text is None, "internal error (text)"
[1406] Fix | Delete
self._last.text = text
[1407] Fix | Delete
self._data = []
[1408] Fix | Delete
[1409] Fix | Delete
##
[1410] Fix | Delete
# Adds text to the current element.
[1411] Fix | Delete
#
[1412] Fix | Delete
# @param data A string. This should be either an 8-bit string
[1413] Fix | Delete
# containing ASCII text, or a Unicode string.
[1414] Fix | Delete
[1415] Fix | Delete
def data(self, data):
[1416] Fix | Delete
self._data.append(data)
[1417] Fix | Delete
[1418] Fix | Delete
##
[1419] Fix | Delete
# Opens a new element.
[1420] Fix | Delete
#
[1421] Fix | Delete
# @param tag The element name.
[1422] Fix | Delete
# @param attrib A dictionary containing element attributes.
[1423] Fix | Delete
# @return The opened element.
[1424] Fix | Delete
# @defreturn Element
[1425] Fix | Delete
[1426] Fix | Delete
def start(self, tag, attrs):
[1427] Fix | Delete
self._flush()
[1428] Fix | Delete
self._last = elem = self._factory(tag, attrs)
[1429] Fix | Delete
if self._elem:
[1430] Fix | Delete
self._elem[-1].append(elem)
[1431] Fix | Delete
self._elem.append(elem)
[1432] Fix | Delete
self._tail = 0
[1433] Fix | Delete
return elem
[1434] Fix | Delete
[1435] Fix | Delete
##
[1436] Fix | Delete
# Closes the current element.
[1437] Fix | Delete
#
[1438] Fix | Delete
# @param tag The element name.
[1439] Fix | Delete
# @return The closed element.
[1440] Fix | Delete
# @defreturn Element
[1441] Fix | Delete
[1442] Fix | Delete
def end(self, tag):
[1443] Fix | Delete
self._flush()
[1444] Fix | Delete
self._last = self._elem.pop()
[1445] Fix | Delete
assert self._last.tag == tag,\
[1446] Fix | Delete
"end tag mismatch (expected %s, got %s)" % (
[1447] Fix | Delete
self._last.tag, tag)
[1448] Fix | Delete
self._tail = 1
[1449] Fix | Delete
return self._last
[1450] Fix | Delete
[1451] Fix | Delete
_sentinel = ['sentinel']
[1452] Fix | Delete
[1453] Fix | Delete
##
[1454] Fix | Delete
# Element structure builder for XML source data, based on the
[1455] Fix | Delete
# <b>expat</b> parser.
[1456] Fix | Delete
#
[1457] Fix | Delete
# @keyparam target Target object. If omitted, the builder uses an
[1458] Fix | Delete
# instance of the standard {@link #TreeBuilder} class.
[1459] Fix | Delete
# @keyparam html Predefine HTML entities. This flag is not supported
[1460] Fix | Delete
# by the current implementation.
[1461] Fix | Delete
# @keyparam encoding Optional encoding. If given, the value overrides
[1462] Fix | Delete
# the encoding specified in the XML file.
[1463] Fix | Delete
# @see #ElementTree
[1464] Fix | Delete
# @see #TreeBuilder
[1465] Fix | Delete
[1466] Fix | Delete
class XMLParser(object):
[1467] Fix | Delete
[1468] Fix | Delete
def __init__(self, html=_sentinel, target=None, encoding=None):
[1469] Fix | Delete
if html is not _sentinel:
[1470] Fix | Delete
warnings.warnpy3k(
[1471] Fix | Delete
"The html argument of XMLParser() is deprecated",
[1472] Fix | Delete
DeprecationWarning, stacklevel=2)
[1473] Fix | Delete
try:
[1474] Fix | Delete
from xml.parsers import expat
[1475] Fix | Delete
except ImportError:
[1476] Fix | Delete
try:
[1477] Fix | Delete
import pyexpat as expat
[1478] Fix | Delete
except ImportError:
[1479] Fix | Delete
raise ImportError(
[1480] Fix | Delete
"No module named expat; use SimpleXMLTreeBuilder instead"
[1481] Fix | Delete
)
[1482] Fix | Delete
parser = expat.ParserCreate(encoding, "}")
[1483] Fix | Delete
if target is None:
[1484] Fix | Delete
target = TreeBuilder()
[1485] Fix | Delete
# underscored names are provided for compatibility only
[1486] Fix | Delete
self.parser = self._parser = parser
[1487] Fix | Delete
self.target = self._target = target
[1488] Fix | Delete
self._error = expat.error
[1489] Fix | Delete
self._names = {} # name memo cache
[1490] Fix | Delete
# callbacks
[1491] Fix | Delete
parser.DefaultHandlerExpand = self._default
[1492] Fix | Delete
parser.StartElementHandler = self._start
[1493] Fix | Delete
parser.EndElementHandler = self._end
[1494] Fix | Delete
parser.CharacterDataHandler = self._data
[1495] Fix | Delete
# optional callbacks
[1496] Fix | Delete
parser.CommentHandler = self._comment
[1497] Fix | Delete
parser.ProcessingInstructionHandler = self._pi
[1498] Fix | Delete
# let expat do the buffering, if supported
[1499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function