Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 10 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
如果它小而简单,那么只需 使用标准库:
这将返回一个 DOM实现标准 文档对象模型 API 的树
如果您以后需要执行复杂的操作,例如架构验证或 XPath 查询,那么我推荐第三方 lxml 模块,它是流行的 libxml2 C 的包装器图书馆。
If it's small and simple then just use the standard library:
This will return a DOM tree implementing the standard Document Object Model API
If you later need to do complex things like schema validation or XPath querying then I recommend the third-party lxml module, which is a wrapper around the popular libxml2 C library.
对于我的大部分任务,我使用了 Minidom 轻量级 DOM 实现,来自官方页面:
For most of my tasks I have used the Minidom Lightweight DOM implementation, from the official page:
这里也是一个关于如何使用 minidom 的非常好的示例以及解释。
Here is also a very good example on how to use minidom along with explanations.
lxml 能满足您的需求吗?这是我使用的第一个 xml 解析工具。
Would lxml suit your needs? Its the first tool I turn to for xml parsing.
几年前,我编写了一个用于处理结构化 XML 的库。它通过一些限制性假设使 XML 变得更简单。
您可以将 XML 用于诸如文字处理文档之类的内容,在这种情况下,您会得到一堆复杂的内容,其中到处都嵌入了 XML 标签;在这种情况下我的图书馆就不好了。
但如果您使用 XML 来处理配置文件之类的内容,我的库就相当方便。您可以定义描述所需 XML 结构的类,完成这些类后,就可以使用一种方法来读取 XML 并解析它。实际的解析是由 xml.dom.minidom 完成的,但随后我的库提取数据并将其放入类中。
最好的部分是:您可以声明一个“Collection”类型,它将是一个 Python 列表,其中包含零个或多个其他 XML 元素。这对于 Atom 或 RSS feed 之类的东西非常有用(这是我设计该库的最初原因)。
网址如下:http://home.avvanta.com/~steveha/xe.html
如果您有任何问题,我很乐意回答。
A few years ago, I wrote a library for working with structured XML. It makes XML simpler by making some limiting assumptions.
You could use XML for something like a word processor document, in which case you have a complicated soup of stuff with XML tags embedded all over the place; in which case my library would not be good.
But if you are using XML for something like a config file, my library is rather convenient. You define classes that describe the structure of the XML you want, and once you have the classes done, there is a method to slurp in XML and parse it. The actual parsing is done by xml.dom.minidom, but then my library extracts the data and puts it in the classes.
The best part: you can declare a "Collection" type that will be a Python list with zero or more other XML elements inside it. This is great for things like Atom or RSS feeds (which was the original reason I designed the library).
Here's the URL: http://home.avvanta.com/~steveha/xe.html
I'd be happy to answer questions if you have any.