【发布时间】:2019-11-18 14:49:21
【问题描述】:
我想用 spyne 将对象导出为 xml 字符串。它使用名为“get_object_as_xml”的函数效果很好,但它没有考虑多态性,如您在以下示例中所见。
我试图添加这一行:
xml_object.polymorphic = True
没有成功。
简单代码:
from spyne.util.xml import xml_object
from spyne.model.complex import ComplexModel
from spyne.model.primitive import Unicode
from spyne.util.xml import get_object_as_xml, get_xml_as_object
from xml.dom.minidom import parseString
from xml.etree.ElementTree import tostring, fromstring
class B(ComplexModel):
_type_info = {
'_b': Unicode,
}
def __init__(self):
super().__init__()
self._b = "b"
class C(B):
_type_info = {
'_c': Unicode,
}
def __init__(self):
super().__init__()
self._c = "c"
class A(ComplexModel):
_type_info = {
'_a': Unicode,
'_b': B,
}
def __init__(self, b=None):
super().__init__()
self._a = 'a'
self._b = b
a = A(C())
# xml_object.polymorphic = True
element_tree = get_object_as_xml(a, A)
xml_string = parseString(tostring(element_tree, encoding='utf-8', method='xml')).toprettyxml(indent=" ")
print(xml_string)
结果 没有 xml_object.polymorphic = True:
<?xml version="1.0" ?>
<A>
<_a>a</_a>
<_b>
<_b>b</_b>
</_b>
</A>
结果 与 xml_object.polymorphic = True:
AppData\Local\Continuum\anaconda3\envs\presto_env\lib\site-packages\spyne\protocol\xml.py", line 722, in gen_members_parent
attrib[XSI_TYPE] = cls.get_type_name_ns(self.app.interface)
AttributeError: 'NoneType' object has no attribute 'interface'
Process finished with exit code 1
我使用的是 spyne 2.13.2a0 版本。是否支持此功能或我做错了什么?
干杯,
【问题讨论】:
标签: spyne