声明式XML处理库
declxml的Python项目详细描述
declxml-声明性xml处理
XML处理变得简单。不再编写和维护数十行或数百行命令序列化和解析逻辑。使用declxml,您可以声明性地定义xml文档的结构,并让declxml处理其余部分。
安装
使用PIP安装
pip install declxml
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)])