Python-Ref > Object oriented programming > Introspection > Deserialization from XML
 
 

<-^^->

Deserialization from XML

Expand/Shrink
  1   import xml.dom.minidom as dom
  2   
  3   class Shape:
  4   
  5     def __init__( self, color=None):
  6       self.color = color
  7   
  8     def get_area( self):
  9       return 0
 10   
 11   
 12   class Rectangle( Shape):
 13   
 14     def __init__( self, color=None, width=None, height=None):
 15       Shape.__init__( self, color=color)
 16       self.width = width
 17       self.height = height
 18   
 19     def get_area( self):
 20       return self.width * self.height
 21   
 22   
 23   class Circle( Shape):
 24   
 25     def __init__( self, color=None, radius=None):
 26       Shape.__init__( self, color=color)
 27       self.radius = radius
 28   
 29     def get_area( self):
 30       return 3.1415 * self.radius**2
 31   
 32   
 33   class Text:
 34   
 35     def __init__( self, color=None, text=None):
 36       self.color = color
 37       self.text = text
 38   
 39   # --------------------------------------------------
 40   
 41   def serialize( objs):
 42     doc = dom.Document()
 43     root = doc.createElement( "Serialization")
 44     doc.appendChild( root)
 45     for o in [o1,o2,o3,o4]:
 46       e = doc.createElement( o.__class__.__name__)
 47       for attr in dir( o):
 48         if not callable( getattr( o, attr)) and not attr.startswith( "_"):
 49           ch = doc.createElement( attr)
 50           ch.appendChild( doc.createTextNode( str( getattr( o, attr))))
 51           e.appendChild( ch)
 52       root.appendChild( e)
 53     return doc
 54   
 55   def deserialize( doc):
 56     objs = []
 57     root = doc.childNodes[0]
 58     for ch in root.childNodes:
 59       if isinstance( ch, dom.Element):
 60         obj_class = globals()[ch.nodeName]
 61         o = obj_class()
 62         for ch2 in ch.childNodes:
 63           if isinstance( ch2, dom.Element):
 64             setattr( o, ch2.nodeName, ch2.childNodes[0].nodeValue)
 65         objs.append( o)
 66     return objs
 67     
 68   
 69   o1 = Rectangle( "black", 2, 3)
 70   o2 = Rectangle( "yellow", 3, 5)
 71   o3 = Circle( "white", 4)
 72   o4 = Text( "green", "Hello world!")
 73   
 74   doc = serialize( [o1,o2,o3,o4])
 75   print doc.toxml()
 76   
 77   objs = deserialize( doc)
 78   for o in objs:
 79     print o, o.__dict__
stdout:
<?xml version="1.0" ?>
<Serialization><Rectangle><color>black</color><height>3</height><width>2</width></Rectangle><Rectangle><color>yellow</color><height>5</height><width>3</width></Rectangle><Circle><color>white</color><radius>4</radius></Circle><Text><color>green</color><text>Hello world!</text></Text></Serialization>
<__main__.Rectangle instance at 0x2b1357cf18c0> {'color': 'black', 'width': '2', 'height': '3'}
<__main__.Rectangle instance at 0x2b1357cf1f80> {'color': 'yellow', 'width': '3', 'height': '5'}
<__main__.Circle instance at 0x2b1357cf1b90> {'color': 'white', 'radius': '4'}
<__main__.Text instance at 0x2b1357cf1128> {'color': 'green', 'text': 'Hello world!'}
Doba běhu: 49.4 ms