【问题标题】:get_object_as_xml not working with polymorphismget_object_as_xml 不适用于多态性
【发布时间】: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


    【解决方案1】:

    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>
    

    【讨论】:

    • xml 结构使用 get_object_as_xml_polymorphic 函数看起来没问题。你知道是否可以从 xml 字符串重新加载这个对象?函数 get_xml_as_object(elt, A) 返回错误“spyne\protocol\xml.py”,第 483 行,在 from_element raise ValidationError(xsi_type) spyne.error.ValidationError: ValidationError(Client.ValidationError: "The value 'C' could未经验证。")"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 2017-03-15
    • 2013-05-06
    相关资源
    最近更新 更多