> From: Tennis Smith <ten...@tripit.com>
> To: generateds-users@lists.sourceforge.net
> Sent: Friday, March 20, 2009 9:42:33 AM
> Subject: [Generateds-users] Export to a variable
>
> Hi,
>
> One thing I'm having trouble getting to work is exporting xml to a
> variable instead of to a file.  Can that be done?
>

All the export methods use a file-like object that supports a
"write()" method.  So, you might copy and modify the parse method
from your generated module, but instead of using sys.stdout, pass
in an instance of StringIO.StringIO.  Then, after export, call the
"getvalue()" method on that StringIO object.

The parse functions in the generated code are intended (1) for
testing, (2) as convenience functions, and (3) for samples.

So, I'd suggest that you copy one of them (probably parse()) from
your generated module, paste it in your own module, then modify it,
and import your generated module.  Here is a sample:

#------------------------------------------------
import sys
import StringIO
from xml.dom import minidom
import tmp2sup

def parseToVariable(inFileName):
    doc = minidom.parse(inFileName)
    rootNode = doc.documentElement
    rootObj = tmp2sup.content.factory()
    rootObj.build(rootNode)
    # Enable Python to collect the space used by the DOM.
    doc = None
    outstream = StringIO.StringIO()
    outstream.write('<?xml version="1.0" ?>\n')
    rootObj.export(outstream, 0, name_="content")
    strcontent = outstream.getvalue()
    print strcontent

def main():
    args = sys.argv[1:]
    if len(args) == 1:
        parseToVariable(args[0])
    else:
        print 'python myprog.py ...'
        sys.exit(1)

if __name__ == '__main__':
    main()
#------------------------------------------------


- Dave

 
-- 

Dave Kuhlman
http://www.rexx.com/~dkuhlman

------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
generateds-users mailing list
generateds-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/generateds-users

Reply via email to