-
Elements and Element Trees (Brief Tutorial) (effbot.org)
This note introduces the Element, SubElement and ElementTree types available in the effbot.org elementtree library. For an overview, with links to articles and more documentation, see the ElementTree Overview page.
Confusing Example
The element type also provides a text attribute, which can be used to hold additional data associated with the element. As the name implies, this attribute is usually used to hold a text string, but it can be used for other, application-specific purposes.
from elementtree.ElementTree import Element
elem = Element("tag")
elem.text = "this element also contains text"
If there is no additional data, this attribute is set to an empty string, or None.
The element type actually provides two attributes that can be used in this way; in addition to text, there’s a similar attribute called tail. It too can contain a text string, an application-specific object, or None. The tail attribute is used to store trailing text nodes when reading mixed-content XML files; text that follows directly after an element are stored in the tail attribute for that element:
<tag><elem>this goes into elem's
text attribute</elem>this goes into
elem's tail attribute</tag>
One tends to correlate the above xml brief with the snippet due to "tag" and "elem" objects. Only by careful observation one can realize that they are unrelated.
Convert ElemntTree to a dictionary, huzzah.
I wrote a small function that will strip out either a list of namespaces (if you pass in ['http://ns1','http://ns2'] as nss) or all namespaces (if you pass in ['*'] as nss). Enjoy:
Why isn't there a parent attribute in Element? I understand this is an explicit choice, but why?