michael@d2m.at  |  +43 (664) 948-8084
You are here: Home Blog 2006 11 14 enabling elementtree

enabling elementtree

by Michael Haubenwallner last modified Jun 18, 2010 11:01 AM

I prefer using elementtree over the xml.dom modules. While it is included with Python2.5 it needs to be installed and configured for current Zope releases.

There is zope.etree by Michael Kerrin for Zope3 (and Five). For older Zope releases you need to resort to ExternalMethods or - even better - you could enable the module for use in PythonScript, DTMLMethod and PageTemplates code. Check HowToAddModulesToRestrictedCode from Zope2 Wiki for background info.

Here is a concrete example of how to enable elementtree for Zope2:

1 - checkout the xml.etree package to your instance Products folder (.../Products/elementtree)

2 - add a new folder e.g. GlobalModules to the Products folder (.../Products/GlobalModules, add other module configurations there too)

3 - inside the GlobalModules folder create a file __init__.py with contents:

from Products.PythonScripts.Utility import allow_module, allow_class
# elementtree declarations
allow_module('Products.elementtree.ElementTree')
from Products.elementtree.ElementTree import _ElementInterface
allow_class(_ElementInterface)
# other module declarations

4 - now restart your Zope instance; in ZMI create a PythonScript object, e.g.:

## Script (Python) "test_elementtree"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
from Products.elementtree.ElementTree import fromstring
print fromstring('<div>test elementtree</div>')
return printed

5 - test it; it should return something like

<Element div at 9e20d4cc>

This setting enables you to use fromstring, tostring, find, findall, getchildren, tag, get, attrib - to name a few API calls.

All done - enjoy elementtree with Zope.

Filed under: , ,