简体   繁体   中英

python iterate xml avoiding namespace

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 ?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM