I haven't tried this in particular, but I can tell you that the
ElementTree API for doing XML processing in Python is going to be
considerably simpler for something like this. ElementTree is available
at http://effbot.org/zone/element-index.htm, and will be in the standard
library starting with 2.5.
Here's a rough translation of your example (barely tested, ymmv, etc...):
from elementtree import ElementTree
def catalog(request):
xml_doc = ElementTree.parse('/usr/web/www/test.xml')
datum = ""
for node in xml_doc.getiterator():
datum = "%s<br />%s - %s" % (datum, node.tag, node.text)
return render_to_response('templates/generic_output.html',
{'output':datum})
Hope that helps.
Nathan
> def catalog(request):
> class countHandler(ContentHandler):
> def __init__(self):
> self.tags = {}
> def startElement(self, name, attr):
> self.tags[name] = 1 + self.tags.get(name,0)
>
> parser = make_parser()
> handler = countHandler()
> parser.setContentHandler(handler)
> parser.parse("/usr/web/www/test.xml")
> tags = handler.tags.keys()
> tags.sort()
>
> datum = ""
> for tag in tags:
> datum = "%s<br />%s - %s" % (datum, tag, handler.tags[tag])
> return render_to_response('templates/generic_output.html',
> {'output':datum})
Mike wrote:
> We have been creating a content management system in Django which
> (despite a few learning bumps) has gone swimmingly. Now, we have
> reached an impasse. We have some applications running elsewhere on the
> site whose xml data we need to access and display from a django view
> function. We read through and experimented with a variety of tutorials
> (since we're all still very new to python) and found several approaches
> that seem to work when written in the shell. Adding them to django
> however has proved to be very difficult. Regardless of what we try, it
> seems impossible to get django to kick out the information we want or
> even to have it generate an error message that would give us some kind
> of feedback. The current iteration that we have experimented with looks
> like this (a shameless ripoff from the Python Cookbook):
>
> from xml.sax import make_parser
> from xml.sax.handler import ContentHandler
>
> def catalog(request):
> class countHandler(ContentHandler):
> def __init__(self):
> self.tags = {}
> def startElement(self, name, attr):
> self.tags[name] = 1 + self.tags.get(name,0)
>
> parser = make_parser()
> handler = countHandler()
> parser.setContentHandler(handler)
> parser.parse("/usr/web/www/test.xml")
> tags = handler.tags.keys()
> tags.sort()
>
> datum = ""
> for tag in tags:
> datum = "%s<br />%s - %s" % (datum, tag, handler.tags[tag])
> return render_to_response('templates/generic_output.html',
> {'output':datum})
>
> We have also tried using minidom with no success. Any pointers in the
> right direction would be incredibly helpful.
>
> Thanks,
> Mike
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" 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/django-users
-~----------~----~----~----~------~----~------~--~---