How to parse an XML file with django.

2,453 views
Skip to first unread message

JFQueralt

unread,
Oct 15, 2008, 6:27:06 PM10/15/08
to Django users
Hi, there.

Being a newbie in django maybe I am just missing something but I don´t
seem to find any information about how to retrieve an XML file and
parse it from django.

My goal is to save some pieces of information in an external file and
then load them into my templates if needed. Obviously, the information
is stored in XML files and I need a way to iterate among the different
items and obtain their values.

Any help will be appreciated.

Cheers.

Jean

James Bennett

unread,
Oct 15, 2008, 7:22:59 PM10/15/08
to django...@googlegroups.com
On Wed, Oct 15, 2008 at 5:27 PM, JFQueralt <JFQu...@gmail.com> wrote:
> Being a newbie in django maybe I am just missing something but I don´t
> seem to find any information about how to retrieve an XML file and
> parse it from django.

"Django" does not have any XML-parsing libraries, because Django is
simply a set of libraries, written in Python, to help you write web
applications in Python. As a result, you probably want to go take a
look at the XML libraries available in Python (including several in
Python's standard library).


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Russell Keith-Magee

unread,
Oct 15, 2008, 7:58:32 PM10/15/08
to django...@googlegroups.com
On Thu, Oct 16, 2008 at 6:27 AM, JFQueralt <JFQu...@gmail.com> wrote:
>
> Hi, there.
>
> Being a newbie in django maybe I am just missing something but I don´t
> seem to find any information about how to retrieve an XML file and
> parse it from django.

Remember - Django isn't a programming language - it's a library built
on a programming language. The Django tools exist to help you deploy a
web site. There are a lot of things that aren't inside that scope,
including parsing formats like XML.

Luckily, Django is built on to of a very good programming language
called Python, and Python has a large selection of excellent XML
parsing tools built in. A google search for "Python XML" will give you
a pretty good list of links and tutorials.

Yours,
Russ Magee %-)

JFQueralt

unread,
Oct 16, 2008, 3:19:22 AM10/16/08
to Django users
Hi, James.

Does that imply I can use Pythong´s XML manipulation libraries in
Django blocks?

There has to be a way to retrieve information fron a file in Django (I
´ve seen official docs on it).
XML is nothing than a structured data file so there should be a way to
retrieve a value and use it in a template.

Should I do it as parameters when invoking the render?
(That way I could retrieve the XML file from the PY module, parse it
and assign a list of variables to the template engine)

Jean

On Oct 16, 1:22 am, "James Bennett" <ubernost...@gmail.com> wrote:

James Bennett

unread,
Oct 16, 2008, 3:26:33 AM10/16/08
to django...@googlegroups.com
On Thu, Oct 16, 2008 at 2:19 AM, JFQueralt <JFQu...@gmail.com> wrote:
> There has to be a way to retrieve information fron a file in Django (I
> ´ve seen official docs on it).
> XML is nothing than a structured data file so there should be a way to
> retrieve a value and use it in a template.

Once again, I think you're confusing ideas here; Django is simply a
set of Python libraries you use to write code, in Python, for web
applications. The code in your Django applications is Python code. Not
some sort of special separate "Django code", but just plain old
ordinary everyday Python code doing the sorts of things plain old
ordinary everyday Python code does: importing things from libraries
and using them.

Once you get over that conceptual problem, I think you'll have a much
easier time of it.

Malcolm Tredinnick

unread,
Oct 16, 2008, 3:39:07 AM10/16/08
to django...@googlegroups.com

On Thu, 2008-10-16 at 00:19 -0700, JFQueralt wrote:
> Hi, James.
>
> Does that imply I can use Pythong´s XML manipulation libraries in
> Django blocks?
>
> There has to be a way to retrieve information fron a file in Django (I
> ´ve seen official docs on it).
> XML is nothing than a structured data file so there should be a way to
> retrieve a value and use it in a template.

I think you're missing something here. Planning to do complex data
manipulation in templates is almost always the wrong place. The view
functions -- the pure Python code -- is where your data manipulations
take place and templates are just a way of specifying how the objects
passed to them are converted to strings (since the result of rendering a
template is a string).

There are limited ways to convert any Python object into a string in a
template. In decreasing order of complexity, they are:

1) Pass it to a template tag.
2) Pass the object through a template filter
3) Call a method on the object that takes no arguments
4) Look up an attribute or dictionary key on the object.
5) Call the objects __unicode__ or __str__ method (this is
essentially a special case of point 3, but it's what happens
automatically when you put "{{ some_var }}" into a template).

So there's not really a question of passing "an XML file" to a template
and then working with it in the template. You only pass Python objects
of some variety to templates. Instead, work with the XML file in the
view (using Python's existing XML libraries, as James and Russell have
mentioned) and convert it to one or more Python objects that can then be
rendered in the templates using the one of the above options.

You could write a template tag that took a Python file object or a
variable containing the name of the file and then used the Python code
behind the template tag to read in the file and do whatever you want
with it. In some circumstances, that might be a useful idiom. However,
if you're just starting out with Django, it will no doubt be easier to
start by doing the initial processing entirely in the view, so that you
only have to look at options 3, 4 and 5 above. Then, after you are
comfortable with that (and right now you seem to have some confusion
about what "Django" means in terms of what it does for you), you might
be able to abstract out somethings into template tags if that makes
things easier (it's not necessarily a given that it will make things
easier, either).

Regards,
Malcolm


kang

unread,
Oct 16, 2008, 3:56:43 AM10/16/08
to django...@googlegroups.com
If you want to parse xml, I recommend feedparser.

http://www.feedparser.org/

wish it helps
--
Stay hungry,Stay foolish.

Russell Keith-Magee

unread,
Oct 16, 2008, 7:03:40 AM10/16/08
to django...@googlegroups.com
On Thu, Oct 16, 2008 at 3:56 PM, kang <areyou...@gmail.com> wrote:
> If you want to parse xml, I recommend feedparser.
>
> http://www.feedparser.org/
>
> wish it helps

You may wish to be a little bit careful giving out advice like that.

Feedparser is a parser for... wait for it... feeds. All feeds are XML
documents, but not all XML documents are feeds. Feedparser is a very
good parser for feeds, but it is completely inappropriate for generic
XML parsing.

Yours,
Russ Magee %-)

kang

unread,
Oct 16, 2008, 7:09:24 AM10/16/08
to django...@googlegroups.com
I know it. Its name tells eveything~

I just want to give him a remmendation if he want to deal with feed.
--
Stay hungry,Stay foolish.

JFQueralt

unread,
Oct 17, 2008, 11:50:24 AM10/17/08
to Django users
Hi, people.

Thanks a lot for the indications. Will work on them and sure will find
a way to implement what I am looking for :-)

Take care.

Jean

On Oct 16, 9:39 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
Reply all
Reply to author
Forward
0 new messages