Creating a network in Python using XML data -
I have an .xml file here
and I want to read the attributes to create Python In a network I could only succeed in reading the properties and I do not know how to proceed. I need to create a bi-directional network using the data in the .xml file. My Pyro code looks like this Can any of you help me with this?
Thanks and Regards
import sys to import xml.dom.minidom xml.dom from xml.dom import minidom xml to .dom.minidom import parse imported OS Read_Data Node.hasAttribute ("id"): node = node.getAttribute ("id") xCoordinates = node = "node" = "node" = "node" .getElementsByTagName ('x') [0] yCoordinates = node.getElementsByTagName ('y') [0] Print "% s:% s% s"% (node.getAttribute ("id"), XCoordinates.childNodes [0] Link: link = link.getAttribute ("Id"); link = link.getAttribute ("Id"). Link = link.getAttribute ("Id") Source: link = link.getAttribute ("Id") source = linkDate.getElementsByTagName ("link") link: Link.getElementsByTagName ('source') [ 0] destination = link.getElementsByTagName ('target') [0] capacity = link.getElementsByTagName ('capability') [0] print "% s -% S to% s:% s"% (link.getAttribute ("id "), Sourc E.childNodes [0] .Data, DestinationBiltonnode [0] .Data, Capacity. ChildNode [0] .data) Demand list = Read_Data.getElementsByTagName (" Demand ") Demand for demand in the list: if Demand = demand.getAttribute ("id") source = demand.getElementsByTagName ('source') [0] destination = demand.getElementsByTagName ('target') [0] demand = demand.getElementsByTagName ('DemandValue') [0] Print "% s requires% s"% (demand.getAttribute ("id"), demand.Childonode [0] .d Ta)
To create a network, use OOPS concept to use Python The code will look like something as shown below. Nodemap is a map with each node and nodes that is linked internally by using the link and demand.
import sys xml.dom.minidom import xml.dom from xml.dom import minidom xml to .dom.minidom import parse imported OS Read_Data = minidom.parse ("germany50.xml") Nodelist = Read_Data.getElementsByTagName ("node") class node: def __init __ (auto, uid, x, y): self.uid = uid self.x = x self.y = y self.linklist = [] self.demandlist = [] Category link: def __init __ (self, UID, source, target, capacity): self.uid = uid self.source = source self. Target = target = Selfkcapacity capacity class demand: def __init __ (self, UID, source, destination, demand value): self.uid = uid self.source = source self.estination = destination self.demandValue = demandValue nodemap = {} For nodes in the node list: If node.hasAttribute ("id"): nodeid = node. The gate attribute ("id") xCoordinates = node.getEle mentsByTagName ('X') [0] yCoordinates = node.getElementsByTagName ('y') [0] nodemap [Nodeid] = node (Nodeid, xCoordinates, yCoordinates) Print "% S:% s% s "% (node.getAttribute (" id "), xCoordinates.childNodes [0] .data, yCoordinates.childNodes [0] .data) linklist = Read_Data.getElementsByTagName (" link ") linklist for if link.hasAttribute ( "ID"): Linkid = Link.getAttribute ( "id") source = link.getElementsByTagName ( 'source') [0] destination = link.getElementsByTagName ( 'target') [0] capacity = Link.getElementsByTagName ('Capability') [0] linkobj = Link (Linked, Source, Destination, Capacity) if Nodemap HAS_ (source): nodemap [source] .linklist.app (linkobject) if nodemap Acs_ki (destination): Nodmap [destination] Klinklist.apend (Linkobje) print "% S -% s to% s:% s"% (link.getAttribute ( "id"), Srotkcildnod [0] Kdeta, destination. Biltonnod [0] Kdeta capacity. Biltonnod [0] Kdeta) demand in demand list for the demand list = Read_Data.getElementsByTagName ( "demand") D: If demand.hasAttribute ( "id"): demand = Demand.gate Attribute ("id") source = demand.getElementsByTagName ('source') [0] destination = demand.getElementsByTagName ('target') [0] Demandval = demand.getElementsByTagName ('demandValue') [0] demandobj = Demanded , Source, Destination, Demandval) If Nodemap HS_ (source): nodemap [source] .demandlist.append (demandonj) if nodemap.has_key (destination): nodemap [destination] .demandlist.append (demandobj) print "% s required % S "% (demand.getAttribute (" id "), Demandval.childNodes [0] .data) Print nodemap
It will be advised to go into Python through OOPS concepts Which you One idea will be how network, graphs, trees can be made in Python.
Hope it helps.
>
Comments
Post a Comment