使用对象方便地创建XML
obj2xml的Python项目详细描述
快速方便地将对象转换为XML。
功能
- 使用XML模板轻松转换对象。
- 支持节点列表和子文档。
- 对象不需要知道XML路径,平面对象可以成为深层XML文档。
- 在创建时动态注入XML。
示例
简单文档:
from obj2xml import XML_Object, XML_Property, XML_TextProperty class MyExampleXML(XML_Object): version = XML_Property(['root', 'version'], 1.0) test_value = XML_Property(['root', 'test-value'], 'Default Value') no_default_value = XML_Property(['root', 'no-default']) text_node = XML_TextProperty(['root', 'text-node-1']) manual_text_node = XML_Property(['root', 'text-node-2', '_text']) obj = MyExampleXML() obj.test_value = 'Another value' obj.no_default_value = 'This value was hidden before' obj.a_text_value = 'Over-rode the default text' obj.text_node = 'This is some text' obj.manual_text_node = 'This is some more text' print(obj) <?xml version="1.0" ?> <root no-default="This value was hidden before" test-value="Another value" version="1.0"> <text-node-2>This is some more text</text-node-2> <text-node-1>This is some text</text-node-1> </root>
有关更多示例,请查看examples目录。