如何将类的实例序列化为 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 版