简体   繁体   English

python迭代xml避免命名空间

[英]python iterate xml avoiding namespace

with my python script i want to iterate my xml file searching a specific element tag.使用我的 python 脚本,我想迭代我的 xml 文件来搜索特定的元素标签。 I have some problem related to the namespace of the root tag.我有一些与根标记的命名空间相关的问题。

Below my XML structure:在我的XML结构下面:

<?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:在我的PYTHON脚本下面:

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 {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.因此,当我遍历它时,所有 tag_1、tag_2 等标签都将包含 {blablabla} 字符串,因此我无法对标签进行任何检查。

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问题是之后的 root 是一个字符串类型,所以我不能覆盖它这样的 Element 类型

How can i print only rootTag ?如何只打印 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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