2

如何将类的实例序列化为 XML 文件并反序列化?


例如。

我有“猫”课:

class Cat:
    __name=''
    __age=0
    __tail_length=0
    __weight=0

    def __init__(self, name, age, tail_length, weight):
        self.__name=name
        self.__age=age
        self.__tail_length=tail_length
        self.__weight=weight

有可能做到吗?

timon=Cat(name='Timon', age=3, tail_length=20, weight=18)
magic_xml_serializer.serialize(destfile='cat-timon.xml', obj=timon)
del(timon)
timon=magic_xml_serializer.deserialize(sourcefile='cat-timon.xml')
print ('yes','no')[type(timon)=='Cat'] #yes

如果可以的话,请写信告诉我怎么做。或者只是给我链接代码示例。谢谢!

PS Python 2.7 版

4

1 回答 1

0

您可以使用 spyne通过服务器 (RPC) 获取 xml 输出。如果您想要的话,spyne 非常适合肥皂(查看您的 C# .NET 背景)

于 2013-09-20T20:04:35.217 回答