从Python解析函数,创建XML对象
xml-python的Python项目详细描述
xml_对象
使用修饰函数从XML创建Python对象来解析节点。在
使用
基本解析器
这个包依赖于“解析器”的创建来解析“节点”。在
考虑一下这个XML:
<root>
<title>Tis is a title</title>
<text>This is some text</text>
</root>
存在三个节点:root
,title
,和{
安装完成后,我们可以使用Builder的from_string
方法打印消息,如下所示:
builder.from_string(xml)
属性
属性可用于向解析器提供关键字参数。考虑Flask示例中的以下示例节点:
<app name="flask-app" host="0.0.0.0" port="8000">
...
</app>```
The app parser in `xml_objects/ext/flask.py` expects `name`, `host`, and `port` as keyword arguments, and provides sensible defaults accordingly:
在@生成器.分析器(“应用程序”) def get_app(parent,text,name=name,host='0.0.0.0',port='4000'): “”“返回烧瓶应用程序。”“” 如果父项不是非父项: 引发运行时错误( '这必须是顶级标记。\n父级:%r'%parent ) app=烧瓶(名称) 应用程序配置['HOST']=主机 应用程序配置['PORT']=int(端口) 退货申请
### Storage
While you could use xml_objects to build GUIs with [wxPython](https://wxpython.org/) for example, xml_objects itself doesn't provide any mechanism to store state, so this must be done by the programmer.
For examples, see `xml_objects/ext/wx.py`.
### Iterators
Sometimes it may be desirable to run code after you have created an object, and all child tags have been processed. For this reason, it is possible to use generators in your parsers:
在@生成器.分析器(“框架”) def get_frame(父级,文本): “”“创建一个框架,然后在其他人完成后完成它。”“” f=框架() 收益率f f、 定稿()
If you yield more than once, RuntimeError will be raised.
- 项目
标签: