Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove namespace with xmltodict in Python

Tags:

python

xml

xmltodict converts XML to a Python dictionary. It supports namespaces. I can follow the example on the homepage and successfully remove a namespace. However, I cannot remove the namespace from my XML and cannot identify why? Here is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<status xmlns:mystatus="http://localhost/mystatus">
<section1
    mystatus:field1="data1"
    mystatus:field2="data2" />
<section2
    mystatus:lineA="outputA"
    mystatus:lineB="outputB" />
</status>

And using:

xmltodict.parse(xml,process_namespaces=True,namespaces={'http://localhost/mystatus':None})

I get:

OrderedDict([(u'status', OrderedDict([(u'section1', OrderedDict([(u'@http://localhost/mystatus:field1', u'data1'), (u'@http://localhost/mystatus:field2', u'data2')])), (u'section2', OrderedDict([(u'@http://localhost/mystatus:lineA', u'outputA'), (u'@http://localhost/mystatus:lineB', u'outputB')]))]))])

instead of:

OrderedDict([(u'status', OrderedDict([(u'section1', OrderedDict([(u'field1', u'data1'), (u'field2', u'data2')])), (u'section2', OrderedDict([(u'lineA', u'outputA'), (u'@lineB', u'outputB')]))]))])

Am I making some simple mistake, or is there something about my XML that prevents the process_namespace modification from working correctly?

like image 605
proximous Avatar asked Nov 04 '14 02:11

proximous


1 Answers

xmltodict is based on expat, so namespaces should applied to the class name, not attribute names:

<?xml version="1.0" encoding="UTF-8"?>
<status xmlns:mystatus="http://localhost/mystatus">
    <mystatus:section1 field1="data1" field2="data2" />
    <mystatus:section2 lineA="outputA" lineB="outputB" />
</status>

When parsed with:

foo = xmltodict.parse(xml,
                      process_namespaces=True,
                      namespaces={'http://localhost/mystatus':None})

outputs:

OrderedDict([(u'status', OrderedDict([(u'section1', OrderedDict([(u'@field1', u'data1'), (u'@field2', u'data2')])), (u'section2', OrderedDict([(u'@lineA', u'outputA'), (u'@lineB', u'outputB')]))]))])

Accessing it is easy:

# Get attribute 'lineA' from class 'section2' from class 'status'
>>> foo.get('status').get('section2').get('@lineA')
u'outputA'

Attribute namespaces are only required when you have multiple attributes of the same name (e.g. multiple id's or multiple prices, etc), in which case, I couldn't get expat or xmltodict to parse it correctly. YMMV though.

like image 184
VooDooNOFX Avatar answered Sep 18 '22 07:09

VooDooNOFX