I also am looking for a such thing, Lawrence, and I suppose
TGWebservices do that,
when I discover why it works only with tg_format=json in my case.
I was reading documentation and seems by default it should returns
data as xml ....


j


On Mar 19, 3:07 pm, LawrenceAfloat <[email protected]> wrote:
> I was wanting to have an easy way to turn any output into an xml
> document as an alternate to exposing the data using json. And managed
> to get this to work. No elegance here.
>
> -- to use
>     @expose('xml:users', content_type='text/xml')
>     # creates a root element called users and fills it from users in
> template_vars
>
> -- add to app_cfg.py
>
> from xml_render import render_xml
> base_config.render_functions.xml = render_xml
>
> -- xml_render.py
> import logging, StringIO
>
> _log = logging.getLogger(__name__)
>
> def render_xml(template_name, template_vars, **kwargs):
>     # turn vars into an xml string.
>     st = StringIO.StringIO()
>
>     def writeElem( obj):
>         if isinstance( obj, dict ):
>             for k in obj:
>                 # create element and recurse
>                 if isinstance( obj[k], list ):
>                     # Add multiple elements. Each value should be a
> dictionary.
>                     for val in obj[k]:
>                         st.write("<%s>" % k)
>                         writeElem(val)
>                         st.write("</%s>" % k)
>
>                 elif isinstance( obj[k], dict ):
>                     # element
>                     st.write("<%s>" % k)
>                     writeElem(obj[k])
>                     st.write("</%s>" % k)
>
>                 else:
>                     st.write("<%s>" % k)
>                     st.write(str(obj[k]))
>                     st.write("</%s>" % k)
>
>         elif isinstance( obj, list ):
>             for val in obj:
>                 writeElem(val)
>         else:
>             st.write(str(obj))
>
>     # main part of function
>     try:
>         st.write("<%s>" % template_name)
>         if template_vars.has_key(template_name):
>             writeElem(template_vars[template_name])
>         st.write("</%s>" % template_name)
>         _log.debug("render_xml %s", st.getvalue() )
>     except Exception,ex:
>         _log.exception("")
>     return st.getvalue()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to