XML在python中行走 [英] XML walking in python

查看:61
本文介绍了XML在python中行走的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,想了解解析xml的知识.对于如何创建通用程序来遍历XML节点集,我还找不到任何很好的示例或解释.

I am new to python and would like to understand parsing xml. I have not been able to find any great examples or explanations of how to create a generic program to walk an XML nodeset.

我希望能够通过名称和值来分类和标识所有元素和属性,而无需任何有关xml模式的信息.我不想依靠标记名称或文本来专门调用元素和属性.

I want to be able to categorize and identify all elements and attributes by name and value, without having any information about the xml schema. I don't want to rely on calling elements and attributes specifically by tag name or text.

有人可以指出正确的方向吗?

Could someone please point me in the right direction?

谢谢

更新:

所要询问的特定问题是:通常,我如何从XML文档中的根节点中递归所有节点,而又不对模式有任何深入的了解."

The specific question that was being asked was, "how do I generally recurse all nodes from the root node in an XML document without having any intimate knowledge about the schema."

当时,我是python的新手,并且了解如何以许多其他语言执行该操作,因此我对不依赖命名节点遍历DOM的任何现实示例感到困惑,这不是我所需要的根本想要.

At the time, being new to python and understanding how to perform that operation in many other languages, I was perplexed by any real world examples that didn't rely on named nodes to traverse the DOM, which isn't what I wanted at all.

希望这可以澄清问题,因为该线程中的信息确实有用.

Hope this clarifies the question, as the information in this thread is indeed useful.

推荐答案

查看 ElementTree 在python帮助中

Check out the documentation of ElementTree on the python help

该页面上的基本代码存根是:

A basic stub of code from that page is:

    import xml.etree.ElementTree as ET
    tree = ET.parse(filename)
    root = tree.getroot()
    for child in root:  
      child.tag, child.attrib

您可以递归地向下运行for child in root:,直到不再有子级为止.

you can keep running for child in root: recursively downward until there aren't any more children.

这篇关于XML在python中行走的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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