#-*-Coding:utf-8-*-' Created on 2010-4-20 @author: Worry-Sith ' import xml.etree.ElementTree as ET import Xml.dom.minido M as minidom from Addrbook.domain Import Person class Converter (object): ' Implement conversion between Python objects and xml ' root = none# root node def __init__ (self): pass @staticmethod def createroot (roottag): ' Create root node ' root = ET. Element (Roottag) return root @staticmethod def getxmlstring (element,defaultencoding= ' utf-8 '): ' returns formatted XML string based on node ' try:rough_string = et.tostring (element, defaultencoding) reparsed = minidom.parsestring (rough_string) return Reparsed.toprettyxml (indent= "", encoding=defaultencoding) Except:print ' getxmlstring: Incoming nodes cannot be converted to XML correctly. Please check that the incoming node is correct ' return ' @staticmethod def classtoelements (classobj,roottag=none): ' "generates nodes based on the object's properties based on an instance of the object passed in. Returns a list of nodes Classobj: instance of an object Roottag: root node name ' Attrs = none# Save object's property Set elelist = [] #节点列表 try:attrs = Classobj.__dict__.keys () # Gets all of the properties of the object (that is, the member variable) except:print ' classtoelements: The incoming object is illegal, the object's properties are not properly fetched ' if Attrs!= None and Len (attrs) >0: #属性存在 for attr in attrs:attrvalue = GetAttr (classobj, attr) #属性值 #属性节点 attre = ET. Element (attr) attre.text = Attrvalue #加入节点列表 elelist.append (attre) return elelist @staticmethod def classtoxml (Classobj, Roottag=none): ' Python custom model class is converted to XML, the conversion succeeds in returning the XML root node, otherwise None Classobj: an instance of the object Roottag: root node name ' Try:classname = classobj.__class__.__name__ #类名 if roottag!= none:root = Converter.createroot (roottag) else:root = Converter.createRoot (classname) Elelist = Converter.classtoelements (Classobj, Roottag) for ele in Elelist:root.append (ele) return root except : print ' Classtoxml: conversion error, please check the incoming object is correct ' return None @staticmethod def collectiontoxml (listobj,roottag= ' list '): ' The collection (list, tuple, dictionary) is converted to XML, and the transformation returns the XML root node successfully, otherwise none "' Try:classname = listobj.__class__.__name__ #类名 root = Converter.createroot (Roottag) if Isinstance (Listobj, list) or isinstance (listobj, tuple): #列表或元组 If Len (listobj) >= 0: For obj in listobj: #迭代列表中的对象 iteme = converter.classtoxml (obj) root.append (iteme) elif isinstance (listobj, dict): #字典 If Len (listobj) >= 0:for key in listobj: #迭代字典中的对象 obj = listobj[key] Iteme = converter.classtoxml (obj) Iteme.set (' key ', key) Root.append (iteme) else:print ' Listtoxml: Conversion error, incoming object: ' +classname+ ' is not a collection type ' return root except: print ' Collectiontoxml: conversion error, collection conversion to XML failed ' return None if __name__ = = ' __main__ ': p1 = person (' Dredfsam ', ' man ', ' 133665 ') P2 = P Erson (' dream ', ' female ', ' r ') P3 = person (' score ', ' Male ', ' sdf ') personlist = {} personlist[' P1 ']= ', p1, personlist[' p2 ']= p2 ' P3 ']= P3 # personlist.append (p1) # personlist.append (p2) # personlist.append (p3) root = Converter.collectiontoxml ( personlist) print converter.getxmlstring (root) # plist = (p1,p2,p3) #{' name ': ' SDF '} # Print type (plist) # Print type (plist) , Isinstance (plist, list)