The version of the ElementTree library included with Python was updated to version 1.3. Some of the new features are:
- The various parsing functions now take a parser keyword argument giving an
XMLParser
instance that will be used. This makes it possible to override the file’s internal encoding:Errors in parsing XML now raise aParseError
exception, whose instances have aposition
attribute containing a (line, column) tuple giving the location of the problem. - ElementTree’s code for converting trees to a string has been significantly reworked, making it roughly twice as fast in many cases. The
ElementTree.write()
andElement.write()
methods now have a method parameter that can be “xml” (the default), “html”, or “text”. HTML mode will output empty elements as<empty></empty>
instead of<empty/>
, and text mode will skip over elements and only output the text chunks. If you set thetag
attribute of an element toNone
but leave its children in place, the element will be omitted when the tree is written out, so you don’t need to do more extensive rearrangement to remove a single element.Namespace handling has also been improved. Allxmlns:<whatever>
declarations are now output on the root element, not scattered throughout the resulting XML. You can set the default namespace for a tree by setting thedefault_namespace
attribute and can register new prefixes withregister_namespace()
. In XML mode, you can use the true/false xml_declaration parameter to suppress the XML declaration. - New
Element
method:extend()
appends the items from a sequence to the element’s children. Elements themselves behave like sequences, so it’s easy to move children from one element to another: - New
Element
method:iter()
yields the children of the element as a generator. It’s also possible to writefor child in elem:
to loop over an element’s children. The existing methodgetiterator()
is now deprecated, as isgetchildren()
which constructs and returns a list of children. - New
Element
method:itertext()
yields all chunks of text that are descendants of the element. For example: - Deprecated: using an element as a Boolean (i.e.,
if elem:
) would return true if the element had any children, or false if there were no children. This behaviour is confusing –None
is false, but so is a childless element? – so it will now trigger aFutureWarning
. In your code, you should be explicit: writelen(elem) != 0
if you’re interested in the number of children, orelem is not None
.
Fredrik Lundh develops ElementTree and produced the 1.3 version; you can read his article describing 1.3 at http://effbot.org/zone/elementtree-13-intro.htm. Florent Xicluna updated the version included with Python, after discussions on python-dev and in bpo-6472.)
No comments:
Post a Comment