[lxml-dev] elementtree as a hierarchical object tree

Hi, I'm new of lxml and XML programming. I would to use etree as a hierarchical object tree: every node element should have a property associated to an object.
import lxml.etree as etree element=etree.fromstring('<TAG>some text</TAG>') element.text="a string"
that's all ok, but:
element.text={'a':'real', 'python':'object'} TypeError: Argument must be string or unicode.
OK, the property name is "text" so some text is expected! My question is: if write a custom element with a property called "object", may I store inside it a reference to an object?

Hi,
I would to use etree as a hierarchical object tree: every node element should have a property associated to an object.
import lxml.etree as etree element=etree.fromstring('<TAG>some text</TAG>') element.text="a string"
that's all ok, but:
element.text={'a':'real', 'python':'object'} TypeError: Argument must be string or unicode.
OK, the property name is "text" so some text is expected!
My question is: if write a custom element with a property called "object", may I store inside it a reference to an object?
This could be helpful: http://codespeak.net/lxml/element_classes.html But be aware that " [...] There is one thing to know up front. Element classes must not have an __init___ or __new__ method. There should not be any internal state either, except for the data stored in the underlying XML tree. [...] " Depending on your use case you might also want to look at http://codespeak.net/lxml/objectify.html Maybe you don't need to reference separate objects but could instead use an XML tree that very much behaves like native python objects. brgds Holger -- Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 - sicherer, schneller und einfacher! http://portal.gmx.net/de/go/atbrowser
participants (2)
-
cgnmm
-
jholg@gmx.de