get_object_as_xml 不使用多态性

get_object_as_xml not working with polymorphism

我想用 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

我使用的是 2.13.2a0 版的 spyne。是否支持此功能或我做错了什么?

干杯,

get_object_as_xml_polymorphic 登陆 PR #619 您可以立即使用它或等待未来几周的下一个版本。

它应该是这样工作的:

#!/usr/bin/env python

from __future__ import print_function

import sys

from lxml import etree
from spyne.util import six

from spyne import ComplexModel, Unicode
from spyne.util.xml import get_object_as_xml_polymorphic


class B(ComplexModel):
    _type_info = [
        ('_b', Unicode(default="b")),
    ]


class C(B):
    _type_info = [
        ('_c', Unicode(default="c")),
    ]


class A(ComplexModel):
    _type_info = [
        ('a', Unicode(subname="_a")),
        ('b', B.customize(subname="_b")),
    ]


a = A(b=C())
elt = get_object_as_xml_polymorphic(a, A, no_namespace=True)
xml_string = etree.tostring(elt, pretty_print=True)
if six.PY2:
    print(xml_string, end="")
else:
    sys.stdout.buffer.write(xml_string)

输出:

<A>
  <b xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="C">
    <_b>b</_b>
    <_c>c</_c>
  </b>
</A>