声明式XML处理库

declxml的Python项目详细描述


declxml-声明性xml处理

PyPI versionPython VersionsBuild StatuscodecovDocumentation StatusLicense

XML处理变得简单。不再编写和维护数十行或数百行命令序列化和解析逻辑。使用declxml,您可以声明性地定义xml文档的结构,并让declxml处理其余部分。

安装

使用PIP安装

pip install declxml

Pipenv

pipenv install declxml

文档

有关详细文档,请参见项目的documentation page

用法

给定一些要处理的xml

<author><name>Robert A. Heinlein</name><birth-year>1907</birth-year><book><title>Starship Troopers</title><published>1959</published></book><book><title>Stranger in a Strange Land</title><published>1961</published></book></author>

创建定义文档结构的declxml处理器

>>>importdeclxmlasxml>>>author_processor=xml.dictionary('author',[...xml.string('name'),...xml.integer('birth-year'),...xml.array(xml.dictionary('book',[...xml.string('title'),...xml.integer('published')...]),alias='books')...])

然后使用该处理器解析XML数据

>>>frompprintimportpprint>>>author_xml="""... <author>...     <name>Robert A. Heinlein</name>...     <birth-year>1907</birth-year>...     <book>...         <title>Starship Troopers</title>...         <published>1959</published>...     </book>...     <book>...         <title>Stranger in a Strange Land</title>...         <published>1961</published>...     </book>... </author>... """>>>pprint(xml.parse_from_string(author_processor,author_xml)){'birth-year':1907,'books':[{'published':1959,'title':'Starship Troopers'},{'published':1961,'title':'Stranger in a Strange Land'}],'name':'Robert A. Heinlein'}

同样的处理器也可用于将数据序列化为XML

>>>author={...'birth-year':1920,...'name':'Issac Asimov',...'books':[...{...'title':'I, Robot',...'published':1950...},...{...'title':'Foundation',...'published':1951...}...]...}>>>print(xml.serialize_to_string(author_processor,author,indent='    '))<?xmlversion="1.0"encoding="utf-8"?><author><name>IssacAsimov</name><birth-year>1920</birth-year><book><title>I,Robot</title><published>1950</published></book><book><title>Foundation</title><published>1951</published></book></author>

要使用对象而不是词典吗?你也可以用declxml来实现这一点

>>>classAuthor:......def__init__(self):...self.name=None...self.birth_year=None...self.books=[]......def__repr__(self):...return'Author(name=\'{}\', birth_year={}, books={})'.format(...self.name,self.birth_year,self.books)>>>classBook:......def__init__(self):...self.title=None...self.published=None......def__repr__(self):...return'Book(title=\'{}\', published={})'.format(self.title,self.published)...>>>author_processor=xml.user_object('author',Author,[...xml.string('name'),...xml.integer('birth-year',alias='birth_year'),...xml.array(xml.user_object('book',Book,[...xml.string('title'),...xml.integer('published')...]),alias='books')...])>>>xml.parse_from_string(author_processor,author_xml)Author(name='Robert A. Heinlein',birth_year=1907,books=[Book(title='Starship Troopers',published=1959),Book(title='Stranger in a Strange Land',published=1961)])

你说有名字的夫妇呢?它们非常有用,declxml还允许您使用它们

>>>fromcollectionsimportnamedtuple>>>Author=namedtuple('Author',['name','birth_year','books'])>>>Book=namedtuple('Book',['title','published'])>>>author_processor=xml.named_tuple('author',Author,[...xml.string('name'),...xml.integer('birth-year',alias='birth_year'),...xml.array(xml.named_tuple('book',Book,[...xml.string('title'),...xml.integer('published')...]),alias='books')...])>>>xml.parse_from_string(author_processor,author_xml)Author(name='Robert A. Heinlein',birth_year=1907,books=[Book(title='Starship Troopers',published=1959),Book(title='Stranger in a Strange Land',published=1961)])

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
jpql中不支持java`TO_CHAR`   给定n个项数的序列的java求和(递归)   java如何使用Spring Security正确获取与用户关联的授权列表?   swing Java 2D图形绘制矩形   java Android将PCM和MP3合并到AAC中   Java中super关键字的作用域和用法   java“Hello World”程序生成了一个异常   java有openjdk8版本吗?如果有,如何获得它?   java创建提供程序失败   java BuildConfigField决定如何定义成员(枚举)   java将经度/纬度转换为X/Y坐标   即使类出现在dex反汇编中,也找不到java活动类   java返回Ljava的字符串值。lang.对象   具有相似模式的java Spring多数据源   java eclipse 安卓不计算表达式   winapi如何使用Java在屏幕上移动Windows?   java刷新jTextField,跟随计时器