python处理xml的三种常见机制

  • dom(随机访问机制)
  • sax(Simple APIs for XML,事件驱动机制)
  • etree

python处理xml的三种包

  • 标准库中的xml
  • Fredrik Lundh 的 ElementTree
  • Stefan Behnel 的 lxml

对以上三种包的介绍和对比

摘录自:http://infohost.nmt.edu/tcc/help/pubs/pylxml/web/index.html

With the continued growth of both Python and XML, there is a plethora(过剩/过多) of packages out there that help you read, generate, and modify XML files from Python scripts. Compared to most of them, the lxml package has two big advantages:

  • Performance. Reading and writing even fairly large XML files takes an almost imperceptible(小得无法察觉的) amount of time.
  • Ease of programming. The lxml package is based on ElementTree, which Fredrik Lundh invented to simplify and streamline XML processing.

lxml is similar in many ways to two other, earlier packages:

  • Fredrik Lundh continues to maintain his original version of ElementTree.
  • xml.etree.ElementTree is now an official part of the Python library. There is a C-language version called cElementTree which may be even faster than lxml for some applications.

However, the author prefers lxml for providing a number of additional features that make life easier. In particular, support for XPath makes it considerably easier to manage more complex XML structures.

标准库中的xml包

摘录自:http://docs.python.org/library/xml.html

The XML handling submodules are:

  • xml.etree.ElementTree: the ElementTree API, a simple and lightweight XML processor
  • xml.dom: the DOM API definition
  • xml.dom.minidom: a minimal DOM implementation
  • xml.dom.pulldom: support for building partial DOM trees
  • xml.sax: SAX2 base classes and convenience functions
  • xml.parsers.expat: the Expat parser binding

ElementTree包

PYPI的介绍:https://pypi.python.org/pypi/elementtree/

The Element type is a flexible container object, designed to store hierarchical data structures in memory. Element structures can be converted to and from XML.

其作者对lxml的推介:http://effbot.org/zone/element-index.htm

There’s also an independent implementation, lxml.etree, based on the well-known libxml2/libxslt libraries. This adds full support for XSLT, XPath, and more.

IBM文档库的介绍文章:XML 问题: 使用 ElementTree,以 Python 语言处理 XML

lxml介绍

摘录自:http://lxml.de/

lxml - XML and HTML with Python

lxml is the most feature-rich and easy-to-use library for processing XML and HTML in the Python language.

The lxml XML toolkit is a Pythonic binding for the C libraries libxml2 and libxslt. It is unique in that it combines the speed and XML feature completeness of these libraries with the simplicity of a native Python API, mostly compatible but superior to the well-known ElementTree API. The latest release works with all CPython versions from 2.6 to 3.6.

总结

一般情况下使用lxml可获得高效率和易用性。

扩展阅读

Python的XML处理方案:

Python XML解析

JAVA的xml方案:

java解析xml的几种方式

lxml教程:

Python XML processing with lxml

命名空间相关:

Parsing XML with lxml and elementtree

XML 命名空间

python处理xml的常用包(lib.xml、ElementTree、lxml)的更多相关文章

  1. python 常用包之xml文件处理

    1,处理xml的包 from xml.etree import ElementTree as ET 2,如何写出xml文件 xml文件和html中的元素很像,有父级子集之说, root = ET.El ...

  2. Python常用内置模块之xml模块

    xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.从结构上,很像HTML超文本标记语言.但他们被设计的目的是不同的,超文本标记语言被设计用来显示 ...

  3. python常用模块之xml模块

    python常用模块之xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,在json还没诞生的年代,大家都是使用xml,目前很多传统公司的系 ...

  4. Python之路(第十六篇)xml模块、datetime模块

    一.xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单, xml比较早,早期许多软件都是用xml,至今很多传统公司如金融行业的很多系统的接口还主要 ...

  5. self._raiseerror(v) File "D:\GameDevelopment\Python27\lib\xml\etree\ElementTree.py", line 1506, in _raiseerror

    D:\BaiDuYun\Plist>python unpack_plist.py lobbyRelieveTraceback (most recent call last): File &quo ...

  6. R语言︱文本挖掘套餐包之——XML+SnowballC+tm包

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- R语言︱文本挖掘套餐包之--XML+tm+Sn ...

  7. 常用模块(xml)

    XML(可扩展性标记语言)是一种非常常用的文件类型,主要用于存储和传输数据.在编程中,对XML的操作也非常常见. 本文根据python库文档中的xml.etree.ElementTree类来进行介绍X ...

  8. C#中常用的读取xml的几种方法(转)

    本文完全来源于http://blog.csdn.net/tiemufeng1122/article/details/6723764,仅作个人学习之用. XML文件是一种常用的文件格式,例如WinFor ...

  9. python项目练习3:万能的XML

    1.目的 如何用XML来表示多种数据,以及如何使用适合XML或SAX的简单API来处理XML文本.目标是通过一个描述各种网页和目录的XML文件生成一个完整的网站. 注:有关XML的描述参见http:/ ...

随机推荐

  1. (转)Android Service 双进程常驻后台(2)

    最近项目用到Service常驻后台,研究了一下发现手Q和微信都是使用了双进程来保证一键清理后自动复活,copy网上双进程Service的例子,再结合onTrimMemory(),基本实现一键清理后自动 ...

  2. Net常用资源小集

    Visual Studio——IDEs工具之王,.NET开发者的必备IDE.Visual Studio提供非常强大的启动工具箱,并且还有一些让人惊喜的插件支持.在去年,微软发布了Visual Stud ...

  3. 授权(Authorization)

    介绍 除了认证服务,laravel还提供了授权服务,laravel同样提供了一个简单的方式去组织授权的逻辑来控制资源的访问.我们提供了各种各样的方法协助你们组织授权的逻辑,这些都在下面的文档之中. 定 ...

  4. 用QT 还是MFC ? (转)

    我曾经使用过QT和MFC来开发过软件,我想和大家分享我使用他们时所体会的不同之处. 我并非一个职业作家,这篇文章可能看起来不如专业的杂志和网站上的那么条理清晰.但是,我在这里是用我自己的语言来表达我自 ...

  5. 彻底弄清c标准库中string.h里的常用函数用法

    在我们平常写的c/c++程序,一些算法题中,我们常常会用到c标准库中string.h文件中的函数,这些函数主要用于处理内存,字符串相关操作,是很有用的工具函数.而且有些时候,在笔试或面试中也会出现让你 ...

  6. JS中的 this

    JS中的 this 变化多端,似乎难以捉摸,但实际上对 this 的解读,还是有一定规律的. 分析this,该如何下手呢?下面有一个函数 function show(){ alert(this); } ...

  7. jQuery.ajax() datatype:“json" 转换失败

    当使用jQuery.ajax() 时,如果设置dataType:"json",如果返回的字符转换出错则不会调用success方法,而进入error方法,控制台中也不会出现错误信息 ...

  8. AngularJS学习-初识

    angularJS定义和特点 1.google前端开源框架 2.MVVM(model view view-model)设计模式 : Model将和ViewModel互动(通过$scope对象),将监听 ...

  9. jquery技巧(持续更新。。)

    (1)集合处理功能         //为索引为0,1,2的元素分别设置不同的字体颜色         $('p').each(function(i){               this.styl ...

  10. Activity的"singleTask"之谜

    官方文档称 以这种方式启动的Activity总是属于一个任务的根Activity.果真如此吗?本文将为你解开Activity的"singleTask"之谜. 任务(Task)是个什 ...