Python-Ref > Object oriented programming > Introspection > Serialization to XML
 
 

<-^^->

Serialization to XML

Expand/Shrink
  1   class Shape:
  2   
  3     def __init__( self, color):
  4       self.color = color
  5   
  6     def get_area( self):
  7       return 0
  8   
  9   
 10   class Rectangle( Shape):
 11   
 12     def __init__( self, color, width, height):
 13       Shape.__init__( self, color)
 14       self.width = width
 15       self.height = height
 16   
 17     def get_area( self):
 18       return self.width * self.height
 19   
 20   
 21   class Circle( Shape):
 22   
 23     def __init__( self, color, radius):
 24       Shape.__init__( self, color)
 25       self.radius = radius
 26   
 27     def get_area( self):
 28       return 3.1415 * self.radius**2
 29   
 30   
 31   class Text:
 32   
 33     def __init__( self, color, text):
 34       self.color = color
 35       self.text = text
 36   
 37   # --------------------------------------------------
 38   
 39   o1 = Rectangle( "black", 2, 3)
 40   o2 = Rectangle( "yellow", 3, 5)
 41   o3 = Circle( "white", 4)
 42   o4 = Text( "green", "Hello world!")
 43   
 44   print "<Serialization>"
 45   print '<Rectangle height="%s" width="%s" color="%s"/>' % (o1.height, o1.width, o1.color)
 46   print '<Rectangle height="%s" width="%s" color="%s"/>' % (o2.height, o2.width, o2.color)
 47   print '<Circle radius="%s" color="%s"/>' % (o3.radius, o2.color)
 48   print '<Text color="%s">%s</Text>' % (o4.color, o4.text)
 49   print "</Serialization>"
stdout:
<Serialization>
<Rectangle height="3" width="2" color="black"/>
<Rectangle height="5" width="3" color="yellow"/>
<Circle radius="4" color="yellow"/>
<Text color="green">Hello world!</Text>
</Serialization>
Doba běhu: 21.8 ms
Expand/Shrink
  1   class Shape:
  2   
  3     def __init__( self, color):
  4       self.color = color
  5   
  6     def get_area( self):
  7       return 0
  8   
  9     def get_xml( self):
 10       return ""
 11   
 12   
 13   class Rectangle( Shape):
 14   
 15     def __init__( self, color, width, height):
 16       Shape.__init__( self, color)
 17       self.width = width
 18       self.height = height
 19   
 20     def get_area( self):
 21       return self.width * self.height
 22   
 23     def get_xml( self):
 24       return '<Rectangle height="%s" width="%s" color="%s"/>' % (self.height, self.width, self.color)
 25   
 26   
 27   class Circle( Shape):
 28   
 29     def __init__( self, color, radius):
 30       Shape.__init__( self, color)
 31       self.radius = radius
 32   
 33     def get_area( self):
 34       return 3.1415 * self.radius**2
 35   
 36     def get_xml( self):
 37       return '<Circle radius="%s" color="%s"/>' % (self.radius, self.color)
 38   
 39   
 40   class Text:
 41   
 42     def __init__( self, color, text):
 43       self.color = color
 44       self.text = text
 45   
 46     def get_xml( self):
 47       return '<Text color="%s">%s</Text>' % (self.color, self.text)
 48   
 49   # --------------------------------------------------
 50   
 51   o1 = Rectangle( "black", 2, 3)
 52   o2 = Rectangle( "yellow", 3, 5)
 53   o3 = Circle( "white", 4)
 54   o4 = Text( "green", "Hello world!")
 55   
 56   print "<Serialization>"
 57   for o in [o1,o2,o3,o4]:
 58     print o.get_xml()
 59   print "</Serialization>"
stdout:
<Serialization>
<Rectangle height="3" width="2" color="black"/>
<Rectangle height="5" width="3" color="yellow"/>
<Circle radius="4" color="white"/>
<Text color="green">Hello world!</Text>
</Serialization>
Doba běhu: 21.6 ms
Expand/Shrink
  1   import xml.dom.minidom as dom
  2   
  3   class Shape:
  4   
  5     def __init__( self, color):
  6       self.color = color
  7   
  8     def get_area( self):
  9       return 0
 10   
 11     def get_xml( self):
 12       return None
 13   
 14   
 15   class Rectangle( Shape):
 16   
 17     def __init__( self, color, width, height):
 18       Shape.__init__( self, color)
 19       self.width = width
 20       self.height = height
 21   
 22     def get_area( self):
 23       return self.width * self.height
 24   
 25     def get_xml( self, doc):
 26       e = doc.createElement( "Rectangle")
 27       e.setAttribute( "height", str( self.height))
 28       e.setAttribute( "width", str( self.width))
 29       e.setAttribute( "color", str( self.color))
 30       return e
 31   
 32   
 33   class Circle( Shape):
 34   
 35     def __init__( self, color, radius):
 36       Shape.__init__( self, color)
 37       self.radius = radius
 38   
 39     def get_area( self):
 40       return 3.1415 * self.radius**2
 41   
 42     def get_xml( self, doc):
 43       e = doc.createElement( "Circle")
 44       e.setAttribute( "radius", str( self.radius))
 45       e.setAttribute( "color", str( self.color))
 46       return e
 47   
 48   
 49   class Text:
 50   
 51     def __init__( self, color, text):
 52       self.color = color
 53       self.text = text
 54   
 55     def get_xml( self, doc):
 56       e = doc.createElement( "Text")
 57       e.appendChild( doc.createTextNode( self.text))
 58       e.setAttribute( "color", str( self.color))
 59       return e
 60   
 61   # --------------------------------------------------
 62   
 63   o1 = Rectangle( "black", 2, 3)
 64   o2 = Rectangle( "yellow", 3, 5)
 65   o3 = Circle( "white", 4)
 66   o4 = Text( "green", "Hello world!")
 67   
 68   doc = dom.Document()
 69   root = doc.createElement( "Serialization")
 70   doc.appendChild( root)
 71   for o in [o1,o2,o3,o4]:
 72     root.appendChild( o.get_xml( doc))
 73   
 74   print doc.toxml()
stdout:
<?xml version="1.0" ?>
<Serialization><Rectangle color="black" height="3" width="2"/><Rectangle color="yellow" height="5" width="3"/><Circle color="white" radius="4"/><Text color="green">Hello world!</Text></Serialization>
Doba běhu: 50.6 ms
Expand/Shrink
  1   class Shape:
  2   
  3     def __init__( self, color):
  4       self.color = color
  5   
  6     def get_area( self):
  7       return 0
  8   
  9   
 10   class Rectangle( Shape):
 11   
 12     def __init__( self, color, width, height):
 13       Shape.__init__( self, color)
 14       self.width = width
 15       self.height = height
 16   
 17     def get_area( self):
 18       return self.width * self.height
 19   
 20   
 21   class Circle( Shape):
 22   
 23     def __init__( self, color, radius):
 24       Shape.__init__( self, color)
 25       self.radius = radius
 26   
 27     def get_area( self):
 28       return 3.1415 * self.radius**2
 29   
 30   
 31   class Text:
 32   
 33     def __init__( self, color, text):
 34       self.color = color
 35       self.text = text
 36   
 37   # --------------------------------------------------
 38   
 39   o1 = Rectangle( "black", 2, 3)
 40   o2 = Rectangle( "yellow", 3, 5)
 41   o3 = Circle( "white", 4)
 42   o4 = Text( "green", "Hello world!")
 43   
 44   import xml.dom.minidom as dom
 45   doc = dom.Document()
 46   root = doc.createElement( "Serialization")
 47   doc.appendChild( root)
 48   for o in [o1,o2,o3,o4]:
 49     e = doc.createElement( o.__class__.__name__)
 50     for attr in dir( o):
 51       if not callable( getattr( o, attr)) and not attr.startswith( "_"):
 52         ch = doc.createElement( attr)
 53         ch.appendChild( doc.createTextNode( str( getattr( o, attr))))
 54         e.appendChild( ch)
 55     root.appendChild( e)
 56   
 57   print doc.toxml()
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>
Doba běhu: 49.1 ms