8. class ElementTree: A complete XML document ................................................................. 17
8.1. ElementTree.find() ............................................................................................... 17
8.2. ElementTree.findall(): Find matching elements ................................................... 17
8.3. ElementTree.findtext(): Retrieve the text content from an element ........................ 18
8.4. ElementTree.getiterator(): Make an iterator ....................................................... 18
8.5. ElementTree.getroot(): Find the root element ....................................................... 18
8.6. ElementTree.xpath(): Evaluate an XPath expression ............................................... 18
8.7. ElementTree.write(): Translate back to XML .......................................................... 19
9. class Element: One element in the tree ............................................................................... 19
9.1. Attributes of an Element instance ................................................................................ 19
9.2. Accessing the list of child elements ............................................................................... 20
9.3. Element.append(): Add a new element child ............................................................ 21
9.4. Element.clear(): Make an element empty ............................................................... 21
9.5. Element.find(): Find a matching sub-element .......................................................... 21
9.6. Element.findall(): Find all matching sub-elements ................................................. 22
9.7. Element.findtext(): Extract text content ................................................................. 23
9.8. Element.get(): Retrieve an attribute value with defaulting ........................................ 23
9.9. Element.getchildren(): Get element children ........................................................ 24
9.10. Element.getiterator(): Make an iterator to walk a subtree ................................... 24
9.11. Element.getroottree(): Find the ElementTree containing this element ............... 26
9.12. Element.insert(): Insert a new child element ......................................................... 26
9.13. Element.items(): Produce attribute names and values ............................................. 26
9.14. Element.iterancestors(): Find an element's ancestors ......................................... 27
9.15. Element.iterchildren(): Find all children ........................................................... 27
9.16. Element.iterdescendants(): Find all descendants ............................................... 28
9.17. Element.itersiblings(): Find other children of the same parent ........................... 28
9.18. Element.keys(): Find all attribute names ................................................................. 29
9.19. Element.remove(): Remove a child element ............................................................ 29
9.20. Element.set(): Set an attribute value ...................................................................... 29
9.21. Element.xpath(): Evaluate an XPath expression ...................................................... 30
10. XPath processing .................................................................................................................. 30
10.1. An XPath example ...................................................................................................... 31
11. The art of Web-scraping: Parsing HTML with Beautiful Soup ................................................... 32
12. Automated validation of input files ........................................................................................ 32
12.1. Validation with a Relax NG schema ............................................................................. 32
12.2. Validation with an XSchema (XSD) schema .................................................................. 33
13. etbuilder.py: A simplified XML builder module ............................................................... 33
13.1. Using the etbuilder module .................................................................................... 34
13.2. CLASS(): Adding class attributes ............................................................................ 35
13.3. FOR(): Adding for attributes .................................................................................... 36
13.4. subElement(): Adding a child element ..................................................................... 36
13.5. addText(): Adding text content to an element ........................................................... 36
14. Implementation of etbuilder ............................................................................................. 36
14.1. Features differing from Lundh's original ...................................................................... 36
14.2. Prologue .................................................................................................................... 36
14.3. CLASS(): Helper function for adding CSS class attributes ......................................... 37
14.4. FOR(): Helper function for adding XHTML for attributes ........................................... 38
14.5. subElement(): Add a child element .......................................................................... 38
14.6. addText(): Add text content to an element ................................................................ 38
14.7. class ElementMaker: The factory class ................................................................... 39
14.8. ElementMaker.__init__(): Constructor ................................................................ 40
14.9. ElementMaker.__call__(): Handle calls to the factory instance .............................. 42
Zoological Data ProcessingPython XML processing with lxml2