Python xml analysis using custom element class

I would like to parse an XML document using the Python module xml.etree.ElementTree. However, I want all the elements in the resulting object tree to have some class methods that I define. This involves creating my own subclass of the Python element class, but I had problems with the parser using its own subclass of the class when parsing, rather than in the built-in class.

For example, let's say I want the nodes in the tree to have a new custommethod () method. To do this, I subclass the element:

class MyElement(xml.etree.ElementTree._Element):

    def custommethod():
        . . . 

Now that I am parsing a tree using

tree = xml.etree.ElementTree.parse(source)

I want all the elements in the tree to have a custommethod () method. So,

tree.getroot.custommethod() 

should not fail.

, - ? Python .parse(), .

+3
5

Dive Into Python . . XML. , , , , .

+1

XML xml.etree.ElementTree.XMLParser :

  • start(self, tag, attrs) , .
  • end(self, tag) .
  • data(self, data) .
  • close(self) .

, node, .. , data(self, data) .

+1

_Element

>>> def customMethod(self):
    print("Done!")

>>> e._Element.customMethod=customMethod
0

, TreeBuilder, :

from xml.etree import cElementTree as etree

class MyCustomElement(etree.Element):
    pass

tb = etree.TreeBuilder(element_factory=MyCustomElement)

: https://github.com/python/cpython/blob/master/Lib/xml/etree/ElementTree.py#L1370

0

Python3 Element . , :

  • __class__/__dict .. .
  • ElementTree C .
  • , , - .

Solution: Create your own parsing function!

import xml.etree.ElementTree
def parse(source):
    """Parse wrapper to build a tree with the extended Node class"""
    treebuilder = xml.etree.ElementTree.TreeBuilder(element_factory=MyElement)
    parser = xml.etree.ElementTree.XMLParser(target=treebuilder)
    tree = xml.etree.ElementTree.parse(source, parser)
    return tree

The above example uses a class instead for each tree object. MyElementElementTree.Element

0
source

All Articles