Parsing XML string with minidom and displaying it in django template

deathtrancer

New Member
I have an XML string like this:\[code\]<?xml version="1.0" encoding="utf-8"?><result> <q> <somestuff>xxx</somestuff> <someotherstuff>yyy</someotherstuff> </q> <items> <res> <first>xxx1</first> <second>xxx1</second> </res> <res> <first>yyy1</first> <second>yyy2</second> </res> <res> <first>zzz</first> <second>zzz</second> </res> (etc.) </items></result>\[/code\]I'd like to parse this string and to be able to handle all its elements within django template. It should be done with minidom or elementtree since I'm not able to install any plugins. It would be perfect to be able to process my data in django template in a way similar to this:\[code\]{% for q in qs %} {{ q.somestuff }}<br /> {{ q.someotherstuff }}{% endfor %}{% for res in result %} {{ res.first }}<br /> {{ res.second }}{% endfor %}\[/code\]So far, I have a function similar to this:\[code\]def index(request): if request.method == 'POST': # if form is posted form = SearchForm(request.POST) if form.is_valid(): url = 'http://some.api.call.which.returns.xml' try: response = urllib2.urlopen(url) res = response.read() except urllib2.HTTPError, error: res = error.read() # qs = ???? # result = ???? # parse xml here and send it to django template return render_to_response('index.html', {'qs': qs, 'result': result}) else: # if form is not posted form = SearchForm() return render_to_response('index.html', { 'form': form }, context_instance=RequestContext(request))\[/code\]
 
Back
Top