XMLを出力するviewの例。xml.sax.saxutils.XMLGeneratorをちょっとだけ拡張したSimplerXMLGeneratorを使うと楽。
from django.http import HttpResponse from django.conf import settings from django.utils.xmlutils import SimplerXMLGenerator def xml_view(request): response = HttpResponse(content_type='application/xml') handler = SimplerXMLGenerator(response, settings.DEFAULT_CHARSET) handler.startDocument() handler.startElement('books', {'myattr': 'attr1'}) handler.addQuickElement('book', 'hogefuga1', {'foo1': 'bar1'}) handler.addQuickElement('book', 'hogefuga2', {'foo2': 'bar2'}) handler.endElement('books') return response
これで出力は、
<?xml version="1.0" encoding="utf-8"?> <books myattr="attr1"><book foo1="bar1">hogefuga1</book><book foo2="bar2">hogefuga2</book></books>
となる。