Sean M. Hester wrote:
> First, kudos to all SQLObject contributors. I've been using it for just over
> 24 hours, and I already can't imagine database persistence without it.
> Second, I am new to the list, but I did search the archives before posting.
> I only offer this disclaimer because I was surprised by how few messages I
> saw containing "XML".
> That being said, I'm looking for a pythonic solution to serialize my
> SQLObject subclasses as xml. At the single SQLObject level this is
> trivial--in fact the included __repr__ definition is just a few
> modifications away from returning a valid xml representation of the object.
> What's not trivial--for me--is representing the related objects' data. I
> would like to represent any *-to-many joins for the object as child elements
> of the original object. For instance, I'd like to have the "xml" property of
> a Person from the personaddress.py example look like the following:
> <person id="1" firstName="John" lastName="Doe">
> <address id="1" street="123" city="Smallsville" state="IL"
Note that "id" is a special attribute in XML, and should be unique in
the document. Maybe instead you should use a dbid attribute.
> If I wanted to custom build each object's xml representation by hand that
> also would be trivial, but I'm trying to abstract this out so all the work
> is done within a generic super-class (which itself would be a sub-class of
> Q1: If I'm creating a new intermediate class for the purpose of adding the
> XML serialization features, what is the best abstract way to navigate the
> one-to-many join data? I see that each SQLObject has a join list and
> dictionary, but I'm getting lost on interrogating the Join objects to get to
> the SQLObjects on the other side of the relationship.
You'll want to look at the objects in self._SO_joinDict, which has
instances of joins.SOJoin. Subclasses of SOJoin, actually.
self._SO_joinDict[key].performJoin(self) should give you the instances
you want. You could get infinite loops if you have RelatedJoins; in
that case, you might want to keep track of the objects that have been
serialized, and if the object has already been serialized just reference
it by id instead of fully serializing it.
> Q2: Reality check: Is there another xml serialization framework, or even
> something with SQLObject, that makes what I'm trying to do a waste of time?
> I've been keeping my eyes open for such an animal for several months, but I
> haven't seen anything that I think would marry happily with SQLObject.
You should give a look at XMLObject http://xmlobject.base-art.net/ ,
which has an API similar to SQLObject, but serializes to and from XML
instead of an RDBMS. What you want to do is somewhere in the middle,
but they might have ideas (and code) over there as well.
Ian Bicking / ianb@... / http://blog.ianbicking.org