Drew's suggestion seems to work well. Making a custom emitter for
each response turns out to do the trick. (Thanks you Drew!)
The only thing that seems to be an issue is responses with XML
elements that go several levels deep. For example, when I make the
simple changes to the emitter code (example 4) to display xml output
in example 1 as a list of messages, the output works great.
However, the problem comes when my messages have other related
objects, such as, for example, multiple tags associated with a
message. Instead of letting me customize the second level of related
elements, it takes the name of the top-level 'message' element. I'd
rather have the output look something like example 3. Does anyone
know how to customize this XML output in this way such that I have
control over all of the XML tags?
Thanks for considering my question.
Joe
example 1:
<response>
<message>....</message>
<message>....</message>
</response>
example 2:
<response>
<message>
<list_of_tags>
<message><name>age</name></message>
<message><name>jjj</name></message>
</list_of_tags>
</message>
<message>
<list_of_tags>
<message><name>age</name></message>
<message><name>jjj</name></message>
</list_of_tags>
</message>
</response>
example 3:
<response>
<message>
<list_of_tags>
<tag><name>age</name></tag>
<tag><name>jjj</name></tag>
</list_of_tags>
</message>
<message>
<list_of_tags>
<tag><name>age</name></tag>
<tag><name>jjj</name></tag>
</list_of_tags>
</message>
</response>
example 4:
class myXMLMessageEmitter(Emitter):
def _to_xml(self, xml, data):
if isinstance(data, (list, tuple)):
for item in data:
xml.startElement("message", {})
self._to_xml(xml, item)
xml.endElement("message")
elif isinstance(data, dict):
for key, value in data.iteritems():
xml.startElement(key, {})
self._to_xml(xml, value)
xml.endElement(key)
else:
xml.characters(smart_unicode(data))
def render(self, request):
stream = StringIO.StringIO()
xml = SimplerXMLGenerator(stream, "utf-8")
xml.startDocument()
xml.startElement("response", {})
self._to_xml(xml, self.construct())
xml.endElement("response")
xml.endDocument()
return stream.getvalue()
Emitter.register('messagexml', myXMLMessageEmitter, 'text/xml;
charset=utf-8')
Mimer.register(lambda *a: None, ('text/xml',))
--
You received this message because you are subscribed to the Google Groups "django-piston" group.
To post to this group, send email to
django...@googlegroups.com.
To unsubscribe from this group, send email to
django-pisto...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-piston?hl=en.