[XML-SIG] RE: XML-SIG digest, Vol 1 #1054 - 2 msgs

Luis P Caamano lcaamano@mindspring.com
Wed, 5 Sep 2001 14:14:55 -0400


Here's my try.  Please double check it 'cause I'm still
not an "xml expert."  :-)  Ahh, and if there's something
that could be done better, please let me know.

Thanks

--
Luis P Caamano
Atlanta, GA USA
lcaamano@mindspring.com

-----Cut Here ---  building.py-------------
# This demo converts a few nested objects into an XML representation

from xml.dom.DOMImplementation import implementation
from xml.dom.ext import PrettyPrint

import types, time, sys

EMPTYNS=''

def object_convert(doc, obj, ele=None):

    # Put the entire object inside an element with the same name as
    # the class.
    cl = doc.createElementNS(EMPTYNS, obj.__class__.__name__ )

    if ele == None:
        doc.appendChild(cl)
    else:
        ele.appendChild(cl)

    L = obj.__dict__.keys()
    L.sort()

    for attr in L:

        # Skip internal attributes (ones that begin with a '_')
        if attr[0] == '_': continue

        value = getattr(obj, attr)
        if type(value) == types.InstanceType:
            # Recursively process subobjects
            object_convert(doc, value, cl)

        else:
            # Convert anything else to a string and put it in an element
            e = doc.createElementNS(EMPTYNS, attr)
            e.appendChild(d.createTextNode(str(value)))
            cl.appendChild(e)


if __name__ == '__main__':
    class Folder: pass
    class Bookmark: pass

    f=Folder()
    f.title = "Folder Title"
    f.version = 1.0
    f.createdTime = time.asctime( time.localtime( time.time() ) )
    f.bookmark = b = Bookmark()
    b.url, b.title = "http://www.python.org", "Python Home Page"

    d = implementation.createDocument(None, None, None)

    object_convert(d, f)

    print "Output from two nested objects:"
    PrettyPrint(d, sys.stdout)

-----End building.py----------------------------------

> -----Original Message-----
> From: xml-sig-admin@python.org [mailto:xml-sig-admin@python.org]On
> Behalf Of xml-sig-request@python.org
> Sent: Wednesday, September 05, 2001 12:01 PM
> To: xml-sig@python.org
> Subject: XML-SIG digest, Vol 1 #1054 - 2 msgs
> 
> 
> Send XML-SIG mailing list submissions to
> 	xml-sig@python.org
> 
> To subscribe or unsubscribe via the World Wide Web, visit
> 	http://mail.python.org/mailman/listinfo/xml-sig
> or, via email, send a message with subject or body 'help' to
> 	xml-sig-request@python.org
> 
> You can reach the person managing the list at
> 	xml-sig-admin@python.org
> 
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of XML-SIG digest..."
> 
> 
> Today's Topics:
> 
>    1. xml/dom/demo/building.py (Luis P Caamano)
>    2. Re: xml/dom/demo/building.py (Martin v. Loewis)
> 
> --__--__--
> 
> Message: 1
> From: "Luis P Caamano" <lcaamano@mindspring.com>
> To: <xml-sig@python.org>
> Date: Tue, 4 Sep 2001 14:02:35 -0400
> Subject: [XML-SIG] xml/dom/demo/building.py
> 
> 
> doesn't work in either 0.6.5 or 0.6.6
> 
> It still uses the old core/builder stuff.
> 
> ----------------------------------
> Luis P. Caamano
> lcaamano@mindspring.com
> Atlanta, GA, USA
> ----------------------------------
> 
> 
> --__--__--
> 
> Message: 2
> Date: Tue, 4 Sep 2001 20:45:58 +0200
> From: "Martin v. Loewis" <martin@loewis.home.cs.tu-berlin.de>
> To: lcaamano@mindspring.com
> CC: xml-sig@python.org
> Subject: Re: [XML-SIG] xml/dom/demo/building.py
> 
> > doesn't work in either 0.6.5 or 0.6.6
> > 
> > It still uses the old core/builder stuff.
> 
> You are right. Would you like to work on updating the example?
> 
> Regards,
> Martin
> 
> 
> 
> 
> End of XML-SIG Digest
>