我使用Spyne框架来提供SOAP Web服务。我需要返回与
非常相似的内容<MyReturnObject>
<MyList>
<MyElement Name="Value1">value2</MyElement>
<MyElement Name="Value2">value3</MyElement>
</MyList>
</MyReturnObject>
我尝试了以下
class MyElement(ComplexModel):
Name = XMLAttribute(String)
class MyReturnObject(ComplexModel):
MyList = Array(MyElement)
但这会导致
<MyReturnObject>
<MyList>
<MyElement Name="Value1"/>
<MyElement Name="Value2"/>
</MyList>
</MyReturnObject>
是否有某种方法可以为给定属性指定文本值? 我非常感谢你的帮助:)。
答案 0 :(得分:0)
这应该有效:
class MyElement(ComplexModel):
Name = XmlAttribute(String)
Data = XmlData(Unicode)
class MyReturnObject(ComplexModel):
MyList = Array(MyElement)
用法:
from lxml import etree
from spyne.util.xml import get_object_as_xml
obj = MyReturnObject(MyList=[
MyElement('value2', Name='Value1'),
MyElement('value3', Name='Value2'),
])
elt = get_object_as_xml(obj, MyReturnObject)
print(etree.tostring(elt, pretty_print=True))
打印:
<MyReturnObject>
<MyList>
<MyElement Name="Value1">value2</MyElement>
<MyElement Name="Value2">value3</MyElement>
</MyList>
</MyReturnObject>