简体   繁体   English

xml.etree还是xml.dom?

[英]xml.etree or xml.dom?

I am trying to read some xml, but I'm not sure which library I should use. 我正在尝试阅读一些xml,但我不确定应该使用哪个库。

What is better xml.etree or xml.dom, and why? 什么是更好的xml.etree或xml.dom,为什么?

Please, explain your answers and give arguments. 请解释你的答案并提出论点。

Also, do you think one of them is going to get deprecated? 另外,你认为其中一个会被弃用吗? Which one? 哪一个?

Neither will be deprecated. 两者都不会被弃用。

ElementTree (xml.etree) is a pythonic API to access XML. ElementTree (xml.etree)是一个访问XML的pythonic API。 DOM (xml.dom) is a cross-platform, language independent standard. DOM (xml.dom)是一种跨平台,独立于语言的标准。

Use ElementTree unless you have a compelling reason to use the XML DOM instead. 除非您有令人信服的理由使用XML DOM,否则请使用ElementTree。 For python code, the ElementTree API is far easier to use than the DOM API. 对于python代码,ElementTree API比DOM API更容易使用。

If you are not averse to installing extra python libraries, you should take a look at the lxml library as well; 如果你不反对安装额外的python库,你也应该看看lxml ; it implements the ElementTree API as well, on top of very fast C libraries, and adds a few more options to work with XML effectively. 它还在非常快的C库之上实现了ElementTree API,并添加了一些选项来有效地使用XML。

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

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