Namespace based XML parsing in Python 2.5

43 Views Asked by At

I can use only Python 2.5 and ElementTree 1.2 I am working on a requirement to parse and read the values of elements from namespace based XML file. I am not keen to use URI's to refer the tag's of the XML in the python code. But I am on to register them once at the start of the code and use them in the subsequent sections.

Please note I tried below approaches they did not work

register_namespace()

namespace = {'abc': 'http://www.example.com'}

Can someone please help me if there is any other way available in Python 2.5 to register the namespaces ?

For Example:

<?xml version = "1.0" encoding = "UTF-8"?>
<cont:contact xmlns:cont = "www.tutorialspoint.com/profile">
 <cont:name>Patil</cont:name>
 <cont:company>XYZ</cont:company>
 <cont:phone>(011) 123-4567</cont:phone>
</cont:contact>

I want to read name from the above xml like this I have many tags in the actual XML I am working on.

If I use URI while referring the xml elements in find() function, code does not look tidier hence I am wondering if there is a way in Python2.5 to register the namespace once and use it everywhere.

In the current setup, I have Python 2.5 only and I have no option of getting it upgraded.

0

There are 0 best solutions below