Python feedparser not using atom/WordPress namespace?

I’m trying to use feedparser (an excellent library) to parse WordPress export files, and a (minor) inconsistency between WordPress version is causing me a huge headache.

WordPress 2.x doesn’t include atom:link tags in the XML output (without_atom_tags.xml). When parsed, namespaced elements are available without the prefix:

Read More
>>> feed = feedparser.parse("without_atom_tags.xml")
>>> print feed.entries[0].comment_status
u'open'

The XML from WordPress 3.x does contain atom:link tags (with_atom_tags.xml), and you must prefix namespace elements:

>>> feed = feedparser.parse("with_atom_tags.xml")
>>> feed.entries[0].wp_comment_status              # <-- Note wp_ prefix
u'open'
>>> feed.entries[0].comment_status
AttributeError: object has no attribute 'comment_status'

Interestingly, the prefixes aren’t needed if I add xmlns:atom="http://www.w3.org/2005/Atom" to the root RSS element (with_atom_tags_and_namespace.xml).

I need to parse all these different formats without modifying the XML. Is feedparser broken, or am I doing it wrong? Can I do this without a bunch of nasty conditional code?

Related posts

Leave a Reply

1 comment