使用XML数据在Python中创建网络

时间:2014-04-28 14:59:06

标签: python xml

我这边有一个.xml文件 http://sndlib.zib.de/coredata.download.action?objectName=germany50&format=xml&objectType=network

我想阅读用Python创建网络的属性。我只能成功地阅读属性,而且我对如何继续下去毫无头绪。我需要使用.xml文件中的数据创建双向网络。我的python代码看起来像这样。你们中的任何人可以帮帮我吗?

谢谢和问候

import sys
import xml.dom.minidom as dom 
import string
from xml.dom import minidom
from xml.dom.minidom import parse
import os
Read_Data = minidom.parse("germany50.xml")
nodelist = Read_Data.getElementsByTagName("node")
for node in nodelist :
    if node.hasAttribute("id"):
        Node = node.getAttribute("id")
    xCoordinates = node.getElementsByTagName('x') [0]
    yCoordinates = node.getElementsByTagName('y') [0]
    print "%s : %s %s" %(node.getAttribute("id"), xCoordinates.childNodes[0].data, yCoordinates.childNodes[0].data)

linklist = Read_Data.getElementsByTagName("link")
for link in linklist :
    if link.hasAttribute("id"):
        Link = link.getAttribute("id")
    Source = link.getElementsByTagName('source') [0]
    Destination = link.getElementsByTagName('target') [0]
    Capacity = link.getElementsByTagName('capacity') [0]
    print "%s - %s to %s: %s" %(link.getAttribute("id"), Source.childNodes[0].data, Destination.childNodes[0].data, Capacity.childNodes[0].data)
demandlist = Read_Data.getElementsByTagName("demand")
for demand in demandlist :
    if demand.hasAttribute("id"):
        Demand = demand.getAttribute("id")
    Source = demand.getElementsByTagName('source') [0]
    Destination = demand.getElementsByTagName('target') [0]
    Demand = demand.getElementsByTagName('demandValue') [0]
    print "%s needs %s" %(demand.getAttribute("id"), Demand.childNodes[0].data)

2 个答案:

答案 0 :(得分:1)

需要使用Python的OOPS概念来创建网络。代码看起来如下所示。 nodemap是每个节点的映射,节点使用链接和需求在内部相互链接。

import sys
import xml.dom.minidom as dom 
import string
from xml.dom import minidom
from xml.dom.minidom import parse
import os
Read_Data = minidom.parse("germany50.xml")
nodelist = Read_Data.getElementsByTagName("node")

class Node:
    def __init__(self,uid,x,y):
        self.uid=uid
        self.x=x
        self.y=y
        self.linklist=[]
        self.demandlist=[]

class Link:
    def __init__(self,uid,source,target,capacity):
        self.uid=uid
        self.source=source
        self.target=target
        self.capacity=capacity

class Demand:
    def __init__(self,uid,source,destination,demandValue):
        self.uid=uid
        self.source=source
        self.destination=destination
        self.demandValue=demandValue


nodemap={}

for node in nodelist :
    if node.hasAttribute("id"):
        Nodeid = node.getAttribute("id")
    xCoordinates = node.getElementsByTagName('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")
for link in linklist :
    if link.hasAttribute("id"):
        Linkid = link.getAttribute("id")
    Source = link.getElementsByTagName('source') [0]
    Destination = link.getElementsByTagName('target') [0]
    Capacity = link.getElementsByTagName('capacity') [0]
    linkobj= Link(Linkid,Source,Destination,Capacity)
    if nodemap.has_key(Source):
        nodemap[Source].linklist.append(linkobj)
    if nodemap.has_key(Destination):
        nodemap[Destination].linklist.append(linkobj)

    print "%s - %s to %s: %s" %(link.getAttribute("id"), Source.childNodes[0].data, Destination.childNodes[0].data, Capacity.childNodes[0].data)
demandlist = Read_Data.getElementsByTagName("demand")
for demand in demandlist :
    if demand.hasAttribute("id"):
        Demandid = demand.getAttribute("id")
    Source = demand.getElementsByTagName('source') [0]
    Destination = demand.getElementsByTagName('target') [0]
    Demandval = demand.getElementsByTagName('demandValue') [0]
    demandobj=Demand(Demandid,Source,Destination,Demandval)
    if nodemap.has_key(Source):
        nodemap[Source].demandlist.append(demandonj)
    if nodemap.has_key(Destination):
        nodemap[Destination].demandlist.append(demandobj)

    print "%s needs %s" %(demand.getAttribute("id"), Demandval.childNodes[0].data)




print nodemap

最好通过Python中的OOPS概念,让您了解如何在Python中形成网络,图形,树。

希望这有帮助。

答案 1 :(得分:1)

for link in linklist :
    if link.hasAttribute("id"):
        Linkid = link.getAttribute("id")
    Source = link.getElementsByTagName('source') [0]
    Destination = link.getElementsByTagName('target') [0]
    Capacity = link.getElementsByTagName('capacity') [0]
    linkobj= Link(Linkid,Source,Destination,Capacity)
    if nodemap.has_key(Source):
        nodemap[Source].linklist.append(linkobj)
    if nodemap.has_key(Destination):
        nodemap[Destination].linklist.append(linkobj)

请注意,如果您的节点列表可能包含[A,B,C,D]并且您的链接可能包含[(A,C),(B,E)],则可能会出现这种情况。在这种情况下,您必须首先创建节点E,然后创建一个链接。我注意到在给定的xml和上面提供的这种情况是为它提供了处理。