Module object_to_xml
[hide private]
[frames] | no frames]

Source Code for Module object_to_xml

 1  #! /usr/local/bin/python 
 2  #-*- coding: utf-8 -*- 
 3   
 4  """object_to_xml 
 5   
 6  Loads a serialized graph object in memory and generates an XML file. 
 7   
 8  Example of an XML file: 
 9   
10  <?xml version="1.0" ?> 
11  <IP-Link> 
12      <ip_packet source_ip="202.30.242.24"> 
13          <ip_packet destination_ip="192.168.1.2" weight="1"/> 
14      </ip_packet> 
15      <ip_packet source_ip="192.168.1.2"> 
16          <ip_packet destination_ip="202.30.242.24" weight="1"/> 
17          <ip_packet destination_ip="200.151.67.119" weight="1"/> 
18          <ip_packet destination_ip="194.154.192.1" weight="1"/> 
19          <ip_packet destination_ip="207.46.134.155" weight="4"/> 
20      </ip_packet> 
21      <ip_packet source_ip="194.154.192.1"> 
22          <ip_packet destination_ip="192.168.1.2" weight="1"/> 
23      </ip_packet> 
24      <ip_packet source_ip="200.151.67.119"> 
25          <ip_packet destination_ip="192.168.1.2" weight="1"/> 
26      </ip_packet> 
27  </IP-Link> 
28  """ 
29   
30  __author__ = "Jerome Hussenet, Cedric Bonhomme" 
31  __version__ = "$Revision: 0.2 $" 
32  __date__ = "$Date: 2009/02/20 $" 
33  __copyright__ = "Copyright (c) 2009 Jerome Hussenet, Copyright (c) 2009 Cedric Bonhomme" 
34  __license__ = "Python" 
35   
36  import os 
37  import sys 
38   
39  import pickle 
40  from xml.dom.minidom import Document 
41   
42   
43 -def object_to_xml(obj_file, xml_file):
44 """Gnerate an XML file.""" 45 dic_obj = open(obj_file, "r") 46 if options.verbose: 47 print "Loading dictionary..." 48 dic_ip = pickle.load(dic_obj) 49 50 if options.verbose: 51 print "Creating XML file..." 52 doc = Document() 53 racine = doc.createElement("IP-Link") 54 doc.appendChild(racine) 55 56 for ip_src in dic_ip: 57 ipsrc = doc.createElement("ip_packet") 58 ipsrc.setAttribute("source_ip", ip_src) 59 racine.appendChild(ipsrc) 60 for ip_dst in dic_ip[ip_src]: 61 ipdst = doc.createElement("ip_packet") 62 ipdst.setAttribute("destination_ip", ip_dst) 63 ipdst.setAttribute("weight", str(dic_ip[ip_src][ip_dst])) 64 ipsrc.appendChild(ipdst) 65 66 # Elegant display of the XML object 67 #print doc.toprettyxml() 68 69 try: 70 file = open(xml_file, 'w') 71 file.write('%s' % doc.toxml().encode('utf-8')) 72 except IOError, e: 73 print "Writting error :", e 74 finally: 75 file.close()
76 77 78 if __name__ == "__main__": 79 # Point of entry in execution mode. 80 from optparse import OptionParser 81 parser = OptionParser() 82 parser.add_option("-i", "--input", dest="obj_file", 83 help="Python serialized object") 84 parser.add_option("-o", "--output", dest="xml_file", 85 help="XML file") 86 parser.add_option("-q", "--quiet", 87 action="store_false", dest="verbose", 88 help="be vewwy quiet (I'm hunting wabbits)") 89 parser.set_defaults(obj_file = './data/dic.pyobj', 90 xml_file = './data/ip.xml', 91 verbose = True) 92 93 (options, args) = parser.parse_args() 94 95 object_to_xml(options.obj_file, options.xml_file) 96