python lxml对象绑定。
lxmlbind的Python项目详细描述
python lxml对象绑定。
[![生成状态](https://travis-ci.org/jessemyers/lxmlbind.png)](https://travis-ci.org/jessemyers/lxmlbind)
这是什么?
[xml.etree][1]库是在python中操作xml的好方法,但在大多数领域中 使用对域实体建模而不是操纵的对象更清楚 直接的XML表示。激励人的例子是[Jenkins][2]XML作业 配置,但任何基于XML的建模都可能存在相同的问题。
此项目尝试使用Python[数据描述符][3]绑定元素树 到Python对象
[1]: http://lxml.de/tutorial.html [2]: http://jenkins-ci.org/ [3]: http://docs.python.org/2/howto/descriptor.html
示例
假设您有一个api,它使用名字和姓氏的xml表示:
xml = “<person><first>John</first><last>Doe</last></person”
lxmlbind使定义映射到此结构的个人对象变得容易:
from lxmlbind.api import Base, Property
- class Person(Base):
- first = Property(“first”) last = Property(“last”)
person的实例包装一个lxml.etree并支持对该结构的操作 通过实例属性:
# create the person from XML person = Person.from_xml(xml)
# get the first name attribute assert person.first == “John”
# set the last name attribute person.last = “Smith”
# print the resulting XML print person.to_xml()
# delete the first name entirely del person.first