使用python ElementTree解析XML文件中的未知元素 [英] parsing an xml file for unknown elements using python ElementTree

查看:70
本文介绍了使用python ElementTree解析XML文件中的未知元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从一个多功能xml文件中提取所有标签名称及其对应的数据。然后将该信息保存到python字典中(例如,tag =键,data =值)。渔获物是标签名称和值未知且数量未知。

I wish to extract all the tag names and their corresponding data from a multi-purpose xml file. Then save that information into a python dictionary (e.g tag = key, data = value). The catch being the tags names and values are unknown and of unknown quantity.

    <some_root_name>
        <tag_x>bubbles</tag_x>
        <tag_y>car</tag_y>
        <tag...>42</tag...>
    </some_root_name>

我正在使用ElementTree,可以成功提取根标记,并可以通过引用标记名称来提取值,但是还没有找到一种方法来简单地遍历标签和数据而不引用标签名称。

I'm using ElementTree and can successfully extract the root tag and can extract values by referencing the tag names, but haven't been able to find a way to simply iterate over the tags and data without referencing a tag name.

任何帮助都将非常有用。

Any help would be great.

谢谢。

推荐答案

from lxml import etree as ET

xmlString = """
    <some_root_name>
        <tag_x>bubbles</tag_x>
        <tag_y>car</tag_y>
        <tag...>42</tag...>
    </some_root_name> """

document = ET.fromstring(xmlString)
for elementtag in document.getiterator():
   print "elementtag name:", elementtag.tag

编辑:
要从文件中读取而不是从字符串中读取

To read from file instead of from string

document = ET.parse("myxmlfile.xml")

这篇关于使用python ElementTree解析XML文件中的未知元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆