mimidom: setting a value of an attribute

Keith Dart kdart at kdart.com
Sat Aug 3 06:43:10 EDT 2002

In article <3D499A73.4020008 at img-online.de>, "Michael Grabietz"
<michael.grabietz at img-online.de> penned these words:

> Hi,
> I experimente with the XML-DOM for Python. I understand after studying
> examples provided in a tutorial how to set elements and the content of
> elements. Now I want to create and set attributes and I fail.
>> # The following fails !
> s1.setAttribute(a1,'attvalue')
> ----------------------------------------------------------------

I also played around with minidom for some things... and it is what drove
me write POM, the Python Object Model for XML. For a Python programmer,
DOM stinks. 

In POM, assuming you have already compiled the DTD with dtd2py program,
you can do this:

import dtds.mydtdmodule as elements

s1 = elements.ELEMENT()
s1.a1 = "attvalue"

print s1

# add child element
s1.add(elements.CONTAINED, att1="att1value")

print s1

# print will show a well-formed XML text
POM currently validates attributes, but not element structure yet. My aim
is to make is auto-validating. A ValidationError exception will be raised
if you try to contstruct an invalid document this way. 

There is also some built in Xpath-like functionality:

child = s1["SUBELEMENT[@att1='att1value']"]

will fetch the added child element.

This is currently contained in the pyNMS package:


It is a work in progress... 

--                           ^
                           \/ \/
                           (O O)
-- --------------------oOOo~(_)~oOOo----------------------------------------
Keith Dart
<mailto:kdart at kdart.com> 
Public key ID: B08B9D2C Public key: <http://www.kdart.com/~kdart/public.key>

More information about the Python-list mailing list