سؤال

In python 2.6, I need to suppress namespace prefixes. I used the solution posted here and it works, however, when I use cElementTree instead for faster parsing, it breaks. From my log:

25/09/2013 20:04:05 'module' object has no attribute '_namespace_map'

Is there some underlying difference between the two implementations? Is there another workaround? The module versions I have:

>>> import xml.etree.ElementTree as et
>>> et.VERSION
'1.2.6'
>>> import xml.etree.cElementTree as cet
>>> cet.VERSION
'1.0.6'
>>> 

Thanks!

هل كانت مفيدة؟

المحلول

There's no reason to expect this to work. jterrace dug around in the internals of ElementTree and came up with a hacky worakround that acts directly on those internals. You're trying to use it on a different implementation, with different internals, so of course it's not going to work.


But… if you look at the source to cElementTree, you can see that the way it handles namespace registration is… to execute some Python code that imports ElementTree and uses its namespace registry. So, instead of this:

import xml.etree.cElementTree as etree

# ...

if namespace_uri not in etree._namespace_map:

You may be able to just import both, then access it off ElementTree instead of cElementTree. For example:

import xml.etree.cElementTree as etree
import xml.etree.ElementTree

# ...

if namespace_uri not in ElementTree._namespace_map:

(Obviously do the same for the other references to it.)

No guarantees this will work. It'll get you past this error, but you'll have to read the code to see if replacing fixtag will have the same effect on cElementTree that you want, or whether there's something else you have to do.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top