Django - Parse XML, output as HTML fragments for i

2019-08-23 10:34发布

问题:

I've been asked to quickly put together a small system that takes a Yahoo XML feed, parses it, and creates a HTML fragment based on it.

At the moment, I'm using Django to handle the URL routing, with some Python script, and outputting a bit of HTML in a (with hardcoded dimensions as inline CSS), that can hopefully be put into an iframe (this part was my half-baked idea).

I've also been asked to throw in a few other bits, like something to parse Twitter XML feeds, Wordpress XML search results etc., that could be re-arranged on a page in iFrames as well.

URLs would be somehting like http://foobar.com/yahoofeed/keyword/dimensions, or /twitter/keyword/dimensions,where keyword is the search keyword, and dimensions is one of some pre-determined sizings (I suppose if there more parameters, I should be passing these as query strings, as opposed to as virtual subiddirectories as part of the URL, right? Thoughts? Pros/Cons of either?).

However, I'm wondering if this is the best way to do this? It does seem a bit hackish to me, but I'm not really sure. Are there any problems with the current approach?

Cheers, Victor

回答1:

Your solution sounds fine. The nature of your problem is glueing together some bits, so it will always be a bit ad-hoc and hackish (and Python is ideally suited for a job like that).

I would use query strings for anything which:

  • doesn't represent a permanent resource
  • represents some kind of query or input option
  • represents an output option

I'd make an exception to this to provide nice URLs for the simple case, and if an option was always required (and always will be required) then you could make it part of the path. So I think keeping the keyword as part of the URL is fine, but dimensions should be a query string option.



回答2:

To parse XML I prefer xml.dom.minidom because of straigtforward usage and it is part of standard python library.

In your case, I think you should use query string instead of long URLs, URLs are flexible but to handle variable length of parameters it's better to use GET or POST. Advantage of this aproach is that you don't have to define URL for each combination of possible variables.

I think you found the best (and easiest) way to do this.