Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python2..../xml/etree
File: ElementTree.py
#
[0] Fix | Delete
# ElementTree
[1] Fix | Delete
# $Id: ElementTree.py 3440 2008-07-18 14:45:01Z fredrik $
[2] Fix | Delete
#
[3] Fix | Delete
# light-weight XML support for Python 2.3 and later.
[4] Fix | Delete
#
[5] Fix | Delete
# history (since 1.2.6):
[6] Fix | Delete
# 2005-11-12 fl added tostringlist/fromstringlist helpers
[7] Fix | Delete
# 2006-07-05 fl merged in selected changes from the 1.3 sandbox
[8] Fix | Delete
# 2006-07-05 fl removed support for 2.1 and earlier
[9] Fix | Delete
# 2007-06-21 fl added deprecation/future warnings
[10] Fix | Delete
# 2007-08-25 fl added doctype hook, added parser version attribute etc
[11] Fix | Delete
# 2007-08-26 fl added new serializer code (better namespace handling, etc)
[12] Fix | Delete
# 2007-08-27 fl warn for broken /tag searches on tree level
[13] Fix | Delete
# 2007-09-02 fl added html/text methods to serializer (experimental)
[14] Fix | Delete
# 2007-09-05 fl added method argument to tostring/tostringlist
[15] Fix | Delete
# 2007-09-06 fl improved error handling
[16] Fix | Delete
# 2007-09-13 fl added itertext, iterfind; assorted cleanups
[17] Fix | Delete
# 2007-12-15 fl added C14N hooks, copy method (experimental)
[18] Fix | Delete
#
[19] Fix | Delete
# Copyright (c) 1999-2008 by Fredrik Lundh. All rights reserved.
[20] Fix | Delete
#
[21] Fix | Delete
# fredrik@pythonware.com
[22] Fix | Delete
# http://www.pythonware.com
[23] Fix | Delete
#
[24] Fix | Delete
# --------------------------------------------------------------------
[25] Fix | Delete
# The ElementTree toolkit is
[26] Fix | Delete
#
[27] Fix | Delete
# Copyright (c) 1999-2008 by Fredrik Lundh
[28] Fix | Delete
#
[29] Fix | Delete
# By obtaining, using, and/or copying this software and/or its
[30] Fix | Delete
# associated documentation, you agree that you have read, understood,
[31] Fix | Delete
# and will comply with the following terms and conditions:
[32] Fix | Delete
#
[33] Fix | Delete
# Permission to use, copy, modify, and distribute this software and
[34] Fix | Delete
# its associated documentation for any purpose and without fee is
[35] Fix | Delete
# hereby granted, provided that the above copyright notice appears in
[36] Fix | Delete
# all copies, and that both that copyright notice and this permission
[37] Fix | Delete
# notice appear in supporting documentation, and that the name of
[38] Fix | Delete
# Secret Labs AB or the author not be used in advertising or publicity
[39] Fix | Delete
# pertaining to distribution of the software without specific, written
[40] Fix | Delete
# prior permission.
[41] Fix | Delete
#
[42] Fix | Delete
# SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
[43] Fix | Delete
# TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
[44] Fix | Delete
# ABILITY AND FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
[45] Fix | Delete
# BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
[46] Fix | Delete
# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
[47] Fix | Delete
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
[48] Fix | Delete
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
[49] Fix | Delete
# OF THIS SOFTWARE.
[50] Fix | Delete
# --------------------------------------------------------------------
[51] Fix | Delete
[52] Fix | Delete
# Licensed to PSF under a Contributor Agreement.
[53] Fix | Delete
# See http://www.python.org/psf/license for licensing details.
[54] Fix | Delete
[55] Fix | Delete
__all__ = [
[56] Fix | Delete
# public symbols
[57] Fix | Delete
"Comment",
[58] Fix | Delete
"dump",
[59] Fix | Delete
"Element", "ElementTree",
[60] Fix | Delete
"fromstring", "fromstringlist",
[61] Fix | Delete
"iselement", "iterparse",
[62] Fix | Delete
"parse", "ParseError",
[63] Fix | Delete
"PI", "ProcessingInstruction",
[64] Fix | Delete
"QName",
[65] Fix | Delete
"SubElement",
[66] Fix | Delete
"tostring", "tostringlist",
[67] Fix | Delete
"TreeBuilder",
[68] Fix | Delete
"VERSION",
[69] Fix | Delete
"XML",
[70] Fix | Delete
"XMLParser", "XMLTreeBuilder",
[71] Fix | Delete
]
[72] Fix | Delete
[73] Fix | Delete
VERSION = "1.3.0"
[74] Fix | Delete
[75] Fix | Delete
##
[76] Fix | Delete
# The <b>Element</b> type is a flexible container object, designed to
[77] Fix | Delete
# store hierarchical data structures in memory. The type can be
[78] Fix | Delete
# described as a cross between a list and a dictionary.
[79] Fix | Delete
# <p>
[80] Fix | Delete
# Each element has a number of properties associated with it:
[81] Fix | Delete
# <ul>
[82] Fix | Delete
# <li>a <i>tag</i>. This is a string identifying what kind of data
[83] Fix | Delete
# this element represents (the element type, in other words).</li>
[84] Fix | Delete
# <li>a number of <i>attributes</i>, stored in a Python dictionary.</li>
[85] Fix | Delete
# <li>a <i>text</i> string.</li>
[86] Fix | Delete
# <li>an optional <i>tail</i> string.</li>
[87] Fix | Delete
# <li>a number of <i>child elements</i>, stored in a Python sequence</li>
[88] Fix | Delete
# </ul>
[89] Fix | Delete
#
[90] Fix | Delete
# To create an element instance, use the {@link #Element} constructor
[91] Fix | Delete
# or the {@link #SubElement} factory function.
[92] Fix | Delete
# <p>
[93] Fix | Delete
# The {@link #ElementTree} class can be used to wrap an element
[94] Fix | Delete
# structure, and convert it from and to XML.
[95] Fix | Delete
##
[96] Fix | Delete
[97] Fix | Delete
import sys
[98] Fix | Delete
import re
[99] Fix | Delete
import warnings
[100] Fix | Delete
[101] Fix | Delete
[102] Fix | Delete
class _SimpleElementPath(object):
[103] Fix | Delete
# emulate pre-1.2 find/findtext/findall behaviour
[104] Fix | Delete
def find(self, element, tag, namespaces=None):
[105] Fix | Delete
for elem in element:
[106] Fix | Delete
if elem.tag == tag:
[107] Fix | Delete
return elem
[108] Fix | Delete
return None
[109] Fix | Delete
def findtext(self, element, tag, default=None, namespaces=None):
[110] Fix | Delete
elem = self.find(element, tag)
[111] Fix | Delete
if elem is None:
[112] Fix | Delete
return default
[113] Fix | Delete
return elem.text or ""
[114] Fix | Delete
def iterfind(self, element, tag, namespaces=None):
[115] Fix | Delete
if tag[:3] == ".//":
[116] Fix | Delete
for elem in element.iter(tag[3:]):
[117] Fix | Delete
yield elem
[118] Fix | Delete
for elem in element:
[119] Fix | Delete
if elem.tag == tag:
[120] Fix | Delete
yield elem
[121] Fix | Delete
def findall(self, element, tag, namespaces=None):
[122] Fix | Delete
return list(self.iterfind(element, tag, namespaces))
[123] Fix | Delete
[124] Fix | Delete
try:
[125] Fix | Delete
from . import ElementPath
[126] Fix | Delete
except ImportError:
[127] Fix | Delete
ElementPath = _SimpleElementPath()
[128] Fix | Delete
[129] Fix | Delete
##
[130] Fix | Delete
# Parser error. This is a subclass of <b>SyntaxError</b>.
[131] Fix | Delete
# <p>
[132] Fix | Delete
# In addition to the exception value, an exception instance contains a
[133] Fix | Delete
# specific exception code in the <b>code</b> attribute, and the line and
[134] Fix | Delete
# column of the error in the <b>position</b> attribute.
[135] Fix | Delete
[136] Fix | Delete
class ParseError(SyntaxError):
[137] Fix | Delete
pass
[138] Fix | Delete
[139] Fix | Delete
# --------------------------------------------------------------------
[140] Fix | Delete
[141] Fix | Delete
##
[142] Fix | Delete
# Checks if an object appears to be a valid element object.
[143] Fix | Delete
#
[144] Fix | Delete
# @param An element instance.
[145] Fix | Delete
# @return A true value if this is an element object.
[146] Fix | Delete
# @defreturn flag
[147] Fix | Delete
[148] Fix | Delete
def iselement(element):
[149] Fix | Delete
# FIXME: not sure about this; might be a better idea to look
[150] Fix | Delete
# for tag/attrib/text attributes
[151] Fix | Delete
return isinstance(element, Element) or hasattr(element, "tag")
[152] Fix | Delete
[153] Fix | Delete
##
[154] Fix | Delete
# Element class. This class defines the Element interface, and
[155] Fix | Delete
# provides a reference implementation of this interface.
[156] Fix | Delete
# <p>
[157] Fix | Delete
# The element name, attribute names, and attribute values can be
[158] Fix | Delete
# either ASCII strings (ordinary Python strings containing only 7-bit
[159] Fix | Delete
# ASCII characters) or Unicode strings.
[160] Fix | Delete
#
[161] Fix | Delete
# @param tag The element name.
[162] Fix | Delete
# @param attrib An optional dictionary, containing element attributes.
[163] Fix | Delete
# @param **extra Additional attributes, given as keyword arguments.
[164] Fix | Delete
# @see Element
[165] Fix | Delete
# @see SubElement
[166] Fix | Delete
# @see Comment
[167] Fix | Delete
# @see ProcessingInstruction
[168] Fix | Delete
[169] Fix | Delete
class Element(object):
[170] Fix | Delete
# <tag attrib>text<child/>...</tag>tail
[171] Fix | Delete
[172] Fix | Delete
##
[173] Fix | Delete
# (Attribute) Element tag.
[174] Fix | Delete
[175] Fix | Delete
tag = None
[176] Fix | Delete
[177] Fix | Delete
##
[178] Fix | Delete
# (Attribute) Element attribute dictionary. Where possible, use
[179] Fix | Delete
# {@link #Element.get},
[180] Fix | Delete
# {@link #Element.set},
[181] Fix | Delete
# {@link #Element.keys}, and
[182] Fix | Delete
# {@link #Element.items} to access
[183] Fix | Delete
# element attributes.
[184] Fix | Delete
[185] Fix | Delete
attrib = None
[186] Fix | Delete
[187] Fix | Delete
##
[188] Fix | Delete
# (Attribute) Text before first subelement. This is either a
[189] Fix | Delete
# string or the value None. Note that if there was no text, this
[190] Fix | Delete
# attribute may be either None or an empty string, depending on
[191] Fix | Delete
# the parser.
[192] Fix | Delete
[193] Fix | Delete
text = None
[194] Fix | Delete
[195] Fix | Delete
##
[196] Fix | Delete
# (Attribute) Text after this element's end tag, but before the
[197] Fix | Delete
# next sibling element's start tag. This is either a string or
[198] Fix | Delete
# the value None. Note that if there was no text, this attribute
[199] Fix | Delete
# may be either None or an empty string, depending on the parser.
[200] Fix | Delete
[201] Fix | Delete
tail = None # text after end tag, if any
[202] Fix | Delete
[203] Fix | Delete
# constructor
[204] Fix | Delete
[205] Fix | Delete
def __init__(self, tag, attrib={}, **extra):
[206] Fix | Delete
attrib = attrib.copy()
[207] Fix | Delete
attrib.update(extra)
[208] Fix | Delete
self.tag = tag
[209] Fix | Delete
self.attrib = attrib
[210] Fix | Delete
self._children = []
[211] Fix | Delete
[212] Fix | Delete
def __repr__(self):
[213] Fix | Delete
return "<Element %s at 0x%x>" % (repr(self.tag), id(self))
[214] Fix | Delete
[215] Fix | Delete
##
[216] Fix | Delete
# Creates a new element object of the same type as this element.
[217] Fix | Delete
#
[218] Fix | Delete
# @param tag Element tag.
[219] Fix | Delete
# @param attrib Element attributes, given as a dictionary.
[220] Fix | Delete
# @return A new element instance.
[221] Fix | Delete
[222] Fix | Delete
def makeelement(self, tag, attrib):
[223] Fix | Delete
return self.__class__(tag, attrib)
[224] Fix | Delete
[225] Fix | Delete
##
[226] Fix | Delete
# (Experimental) Copies the current element. This creates a
[227] Fix | Delete
# shallow copy; subelements will be shared with the original tree.
[228] Fix | Delete
#
[229] Fix | Delete
# @return A new element instance.
[230] Fix | Delete
[231] Fix | Delete
def copy(self):
[232] Fix | Delete
elem = self.makeelement(self.tag, self.attrib)
[233] Fix | Delete
elem.text = self.text
[234] Fix | Delete
elem.tail = self.tail
[235] Fix | Delete
elem[:] = self
[236] Fix | Delete
return elem
[237] Fix | Delete
[238] Fix | Delete
##
[239] Fix | Delete
# Returns the number of subelements. Note that this only counts
[240] Fix | Delete
# full elements; to check if there's any content in an element, you
[241] Fix | Delete
# have to check both the length and the <b>text</b> attribute.
[242] Fix | Delete
#
[243] Fix | Delete
# @return The number of subelements.
[244] Fix | Delete
[245] Fix | Delete
def __len__(self):
[246] Fix | Delete
return len(self._children)
[247] Fix | Delete
[248] Fix | Delete
def __nonzero__(self):
[249] Fix | Delete
warnings.warn(
[250] Fix | Delete
"The behavior of this method will change in future versions. "
[251] Fix | Delete
"Use specific 'len(elem)' or 'elem is not None' test instead.",
[252] Fix | Delete
FutureWarning, stacklevel=2
[253] Fix | Delete
)
[254] Fix | Delete
return len(self._children) != 0 # emulate old behaviour, for now
[255] Fix | Delete
[256] Fix | Delete
##
[257] Fix | Delete
# Returns the given subelement, by index.
[258] Fix | Delete
#
[259] Fix | Delete
# @param index What subelement to return.
[260] Fix | Delete
# @return The given subelement.
[261] Fix | Delete
# @exception IndexError If the given element does not exist.
[262] Fix | Delete
[263] Fix | Delete
def __getitem__(self, index):
[264] Fix | Delete
return self._children[index]
[265] Fix | Delete
[266] Fix | Delete
##
[267] Fix | Delete
# Replaces the given subelement, by index.
[268] Fix | Delete
#
[269] Fix | Delete
# @param index What subelement to replace.
[270] Fix | Delete
# @param element The new element value.
[271] Fix | Delete
# @exception IndexError If the given element does not exist.
[272] Fix | Delete
[273] Fix | Delete
def __setitem__(self, index, element):
[274] Fix | Delete
# if isinstance(index, slice):
[275] Fix | Delete
# for elt in element:
[276] Fix | Delete
# assert iselement(elt)
[277] Fix | Delete
# else:
[278] Fix | Delete
# assert iselement(element)
[279] Fix | Delete
self._children[index] = element
[280] Fix | Delete
[281] Fix | Delete
##
[282] Fix | Delete
# Deletes the given subelement, by index.
[283] Fix | Delete
#
[284] Fix | Delete
# @param index What subelement to delete.
[285] Fix | Delete
# @exception IndexError If the given element does not exist.
[286] Fix | Delete
[287] Fix | Delete
def __delitem__(self, index):
[288] Fix | Delete
del self._children[index]
[289] Fix | Delete
[290] Fix | Delete
##
[291] Fix | Delete
# Adds a subelement to the end of this element. In document order,
[292] Fix | Delete
# the new element will appear after the last existing subelement (or
[293] Fix | Delete
# directly after the text, if it's the first subelement), but before
[294] Fix | Delete
# the end tag for this element.
[295] Fix | Delete
#
[296] Fix | Delete
# @param element The element to add.
[297] Fix | Delete
[298] Fix | Delete
def append(self, element):
[299] Fix | Delete
# assert iselement(element)
[300] Fix | Delete
self._children.append(element)
[301] Fix | Delete
[302] Fix | Delete
##
[303] Fix | Delete
# Appends subelements from a sequence.
[304] Fix | Delete
#
[305] Fix | Delete
# @param elements A sequence object with zero or more elements.
[306] Fix | Delete
# @since 1.3
[307] Fix | Delete
[308] Fix | Delete
def extend(self, elements):
[309] Fix | Delete
# for element in elements:
[310] Fix | Delete
# assert iselement(element)
[311] Fix | Delete
self._children.extend(elements)
[312] Fix | Delete
[313] Fix | Delete
##
[314] Fix | Delete
# Inserts a subelement at the given position in this element.
[315] Fix | Delete
#
[316] Fix | Delete
# @param index Where to insert the new subelement.
[317] Fix | Delete
[318] Fix | Delete
def insert(self, index, element):
[319] Fix | Delete
# assert iselement(element)
[320] Fix | Delete
self._children.insert(index, element)
[321] Fix | Delete
[322] Fix | Delete
##
[323] Fix | Delete
# Removes a matching subelement. Unlike the <b>find</b> methods,
[324] Fix | Delete
# this method compares elements based on identity, not on tag
[325] Fix | Delete
# value or contents. To remove subelements by other means, the
[326] Fix | Delete
# easiest way is often to use a list comprehension to select what
[327] Fix | Delete
# elements to keep, and use slice assignment to update the parent
[328] Fix | Delete
# element.
[329] Fix | Delete
#
[330] Fix | Delete
# @param element What element to remove.
[331] Fix | Delete
# @exception ValueError If a matching element could not be found.
[332] Fix | Delete
[333] Fix | Delete
def remove(self, element):
[334] Fix | Delete
# assert iselement(element)
[335] Fix | Delete
self._children.remove(element)
[336] Fix | Delete
[337] Fix | Delete
##
[338] Fix | Delete
# (Deprecated) Returns all subelements. The elements are returned
[339] Fix | Delete
# in document order.
[340] Fix | Delete
#
[341] Fix | Delete
# @return A list of subelements.
[342] Fix | Delete
# @defreturn list of Element instances
[343] Fix | Delete
[344] Fix | Delete
def getchildren(self):
[345] Fix | Delete
warnings.warn(
[346] Fix | Delete
"This method will be removed in future versions. "
[347] Fix | Delete
"Use 'list(elem)' or iteration over elem instead.",
[348] Fix | Delete
DeprecationWarning, stacklevel=2
[349] Fix | Delete
)
[350] Fix | Delete
return self._children
[351] Fix | Delete
[352] Fix | Delete
##
[353] Fix | Delete
# Finds the first matching subelement, by tag name or path.
[354] Fix | Delete
#
[355] Fix | Delete
# @param path What element to look for.
[356] Fix | Delete
# @keyparam namespaces Optional namespace prefix map.
[357] Fix | Delete
# @return The first matching element, or None if no element was found.
[358] Fix | Delete
# @defreturn Element or None
[359] Fix | Delete
[360] Fix | Delete
def find(self, path, namespaces=None):
[361] Fix | Delete
return ElementPath.find(self, path, namespaces)
[362] Fix | Delete
[363] Fix | Delete
##
[364] Fix | Delete
# Finds text for the first matching subelement, by tag name or path.
[365] Fix | Delete
#
[366] Fix | Delete
# @param path What element to look for.
[367] Fix | Delete
# @param default What to return if the element was not found.
[368] Fix | Delete
# @keyparam namespaces Optional namespace prefix map.
[369] Fix | Delete
# @return The text content of the first matching element, or the
[370] Fix | Delete
# default value no element was found. Note that if the element
[371] Fix | Delete
# is found, but has no text content, this method returns an
[372] Fix | Delete
# empty string.
[373] Fix | Delete
# @defreturn string
[374] Fix | Delete
[375] Fix | Delete
def findtext(self, path, default=None, namespaces=None):
[376] Fix | Delete
return ElementPath.findtext(self, path, default, namespaces)
[377] Fix | Delete
[378] Fix | Delete
##
[379] Fix | Delete
# Finds all matching subelements, by tag name or path.
[380] Fix | Delete
#
[381] Fix | Delete
# @param path What element to look for.
[382] Fix | Delete
# @keyparam namespaces Optional namespace prefix map.
[383] Fix | Delete
# @return A list or other sequence containing all matching elements,
[384] Fix | Delete
# in document order.
[385] Fix | Delete
# @defreturn list of Element instances
[386] Fix | Delete
[387] Fix | Delete
def findall(self, path, namespaces=None):
[388] Fix | Delete
return ElementPath.findall(self, path, namespaces)
[389] Fix | Delete
[390] Fix | Delete
##
[391] Fix | Delete
# Finds all matching subelements, by tag name or path.
[392] Fix | Delete
#
[393] Fix | Delete
# @param path What element to look for.
[394] Fix | Delete
# @keyparam namespaces Optional namespace prefix map.
[395] Fix | Delete
# @return An iterator or sequence containing all matching elements,
[396] Fix | Delete
# in document order.
[397] Fix | Delete
# @defreturn a generated sequence of Element instances
[398] Fix | Delete
[399] Fix | Delete
def iterfind(self, path, namespaces=None):
[400] Fix | Delete
return ElementPath.iterfind(self, path, namespaces)
[401] Fix | Delete
[402] Fix | Delete
##
[403] Fix | Delete
# Resets an element. This function removes all subelements, clears
[404] Fix | Delete
# all attributes, and sets the <b>text</b> and <b>tail</b> attributes
[405] Fix | Delete
# to None.
[406] Fix | Delete
[407] Fix | Delete
def clear(self):
[408] Fix | Delete
self.attrib.clear()
[409] Fix | Delete
self._children = []
[410] Fix | Delete
self.text = self.tail = None
[411] Fix | Delete
[412] Fix | Delete
##
[413] Fix | Delete
# Gets an element attribute. Equivalent to <b>attrib.get</b>, but
[414] Fix | Delete
# some implementations may handle this a bit more efficiently.
[415] Fix | Delete
#
[416] Fix | Delete
# @param key What attribute to look for.
[417] Fix | Delete
# @param default What to return if the attribute was not found.
[418] Fix | Delete
# @return The attribute value, or the default value, if the
[419] Fix | Delete
# attribute was not found.
[420] Fix | Delete
# @defreturn string or None
[421] Fix | Delete
[422] Fix | Delete
def get(self, key, default=None):
[423] Fix | Delete
return self.attrib.get(key, default)
[424] Fix | Delete
[425] Fix | Delete
##
[426] Fix | Delete
# Sets an element attribute. Equivalent to <b>attrib[key] = value</b>,
[427] Fix | Delete
# but some implementations may handle this a bit more efficiently.
[428] Fix | Delete
#
[429] Fix | Delete
# @param key What attribute to set.
[430] Fix | Delete
# @param value The attribute value.
[431] Fix | Delete
[432] Fix | Delete
def set(self, key, value):
[433] Fix | Delete
self.attrib[key] = value
[434] Fix | Delete
[435] Fix | Delete
##
[436] Fix | Delete
# Gets a list of attribute names. The names are returned in an
[437] Fix | Delete
# arbitrary order (just like for an ordinary Python dictionary).
[438] Fix | Delete
# Equivalent to <b>attrib.keys()</b>.
[439] Fix | Delete
#
[440] Fix | Delete
# @return A list of element attribute names.
[441] Fix | Delete
# @defreturn list of strings
[442] Fix | Delete
[443] Fix | Delete
def keys(self):
[444] Fix | Delete
return self.attrib.keys()
[445] Fix | Delete
[446] Fix | Delete
##
[447] Fix | Delete
# Gets element attributes, as a sequence. The attributes are
[448] Fix | Delete
# returned in an arbitrary order. Equivalent to <b>attrib.items()</b>.
[449] Fix | Delete
#
[450] Fix | Delete
# @return A list of (name, value) tuples for all attributes.
[451] Fix | Delete
# @defreturn list of (string, string) tuples
[452] Fix | Delete
[453] Fix | Delete
def items(self):
[454] Fix | Delete
return self.attrib.items()
[455] Fix | Delete
[456] Fix | Delete
##
[457] Fix | Delete
# Creates a tree iterator. The iterator loops over this element
[458] Fix | Delete
# and all subelements, in document order, and returns all elements
[459] Fix | Delete
# with a matching tag.
[460] Fix | Delete
# <p>
[461] Fix | Delete
# If the tree structure is modified during iteration, new or removed
[462] Fix | Delete
# elements may or may not be included. To get a stable set, use the
[463] Fix | Delete
# list() function on the iterator, and loop over the resulting list.
[464] Fix | Delete
#
[465] Fix | Delete
# @param tag What tags to look for (default is to return all elements).
[466] Fix | Delete
# @return An iterator containing all the matching elements.
[467] Fix | Delete
# @defreturn iterator
[468] Fix | Delete
[469] Fix | Delete
def iter(self, tag=None):
[470] Fix | Delete
if tag == "*":
[471] Fix | Delete
tag = None
[472] Fix | Delete
if tag is None or self.tag == tag:
[473] Fix | Delete
yield self
[474] Fix | Delete
for e in self._children:
[475] Fix | Delete
for e in e.iter(tag):
[476] Fix | Delete
yield e
[477] Fix | Delete
[478] Fix | Delete
# compatibility
[479] Fix | Delete
def getiterator(self, tag=None):
[480] Fix | Delete
# Change for a DeprecationWarning in 1.4
[481] Fix | Delete
warnings.warn(
[482] Fix | Delete
"This method will be removed in future versions. "
[483] Fix | Delete
"Use 'elem.iter()' or 'list(elem.iter())' instead.",
[484] Fix | Delete
PendingDeprecationWarning, stacklevel=2
[485] Fix | Delete
)
[486] Fix | Delete
return list(self.iter(tag))
[487] Fix | Delete
[488] Fix | Delete
##
[489] Fix | Delete
# Creates a text iterator. The iterator loops over this element
[490] Fix | Delete
# and all subelements, in document order, and returns all inner
[491] Fix | Delete
# text.
[492] Fix | Delete
#
[493] Fix | Delete
# @return An iterator containing all inner text.
[494] Fix | Delete
# @defreturn iterator
[495] Fix | Delete
[496] Fix | Delete
def itertext(self):
[497] Fix | Delete
tag = self.tag
[498] Fix | Delete
if not isinstance(tag, basestring) and tag is not None:
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function