python iterate xml avoiding namespace

156 views Asked by At

with my python script i want to iterate my xml file searching a specific element tag. I have some problem related to the namespace of the root tag.

Below my XML structure:

<?xml version="1.0" ?>
<rootTag xmlns="blablabla">
    <tag_1>
        <sub_tag_1>..something..</sub_tag_1>
    </tag_1>
    <tag_2>
        <sub_tag_2>..something..</sub_tag_2>
    </tag_2>
    ...and so on...
</rootTag>

Below my PYTHON script:

import xml.etree.ElementTree as ET

root = ET.fromstring(xml_taken_from_web)
print(root.tag)

The problem is that output of print is:

{blablabla}rootTag

so when i iter over it all the tag_1, tag_2, and so on tags will have the {blablabla} string so i'm not able to make any check on the tag.

I tried using regular expression in this way

root = re.sub('^{.*?}', '', root.tag)

the problem is that root after that is a string type and so i cannot over it such an Element type

How can i print only rootTag ?

1

There are 1 answers

7
U13-Forward On BEST ANSWER

With that just use:

import xml.etree.ElementTree as ET
from lxml import etree

root = ET.fromstring(xml_taken_from_web)
print(etree.QName(root.tag).localname)