[XML-SIG] The Zen of DOM
Thu, 06 Apr 2000 00:33:01 -0600
> Looking for spirtual guidance about the right way to do things...
You're really looking for two "right ways" to do two very different things.
> It seems to me that one could use Python's extreme flexibility to take a
> generic approach to tree-building, and see if there was a class available
> corresponding to a particular tag before creating some generic node; if so,
> create it, pass it the available attributes, then pass child nodes to an
> add() method so it could organize them itself. Then I could magically end
> up with a notation like...
> ...without having to write lots of new stuff in the parser as well as the
> application class hierarchy every time. Or at least to navigate the tree
> using generic node/child notation, but get my own class instances attached
> at each point.
To my moderately trained eye, this has less to do with DOM or even XML than it
does abstract models. Grove advocates would tell you that what you want is a
grove model for your hierarchical data. But then again you could also do it
with a very simple translation layer on top of the DOM.
A SAX handler or simple NodeIterator that converts nodes or events from your
serialization into application objects representing your direct semantics
would zip the trick nicely. We use this model at Fourthought for conversion
between XML forms (such as XBEL) and persistent object stubs (such as in our
demo bookmark manager app for 4ODS).
> To turn this on its head, there must be a generic way to turn a Python class
> instance into XML, and unserialize it again later.
This is a different and simpler question. Ken MacLeod has your answer.
> Has anyone actually worked on this? Is there a solution lurking in the
> package somewhere? Or is the preferred approach to get a DOM tree, then
> walk through it building my own objects?
There are simple trade-offs to consider between speed, code-simplicity and
firstname.lastname@example.org (303)583-9900 x 101
Fourthought, Inc. http://Fourthought.com
4735 East Walnut St, Ste. C, Boulder, CO 80301-9036
Software-engineering, knowledge-management, XML, CORBA, Linux, Python