Wouter Milants
Wouter Milants

Reputation: 3

undirected weighted graph to xml

I want to represent an undirected graph, like this one :

http://cnx.org/content/m29399/latest/Picture%202.png

into an xml file. I'm not really sure how to begin.

EDIT: I want the data in xml format, so I can reconstruct the graph using the xml.

thanks in advance

Upvotes: 0

Views: 379

Answers (4)

Tamás
Tamás

Reputation: 48041

IMHO you shouldn't try to reinvent the wheel, just use one of the already existing XML-based graph formats: GraphML, XGMML or GXL. GraphML seems to be the easiest to start with.

Upvotes: 1

Wouter Milants
Wouter Milants

Reputation: 3

I kept trying things and this seems to be working :

   <Nodes>
       <Node>
           <Name>A</Name>
       </Node> 
       <Node>
           <Name>B</Name>
       </Node> 
        ....
   </Nodes>

   <Routes>
       <Route>
          <from>A</from>
          <to>B</to>
          <cost>7</cost>
        </Route>
        ....
   </Routes>

Probably not the best way but this works for me :)enter code here

Upvotes: 0

arayq2
arayq2

Reputation: 2554

A graph consists of vertices and edges. So at the top level the XML description should look like this

<graph>
    <vertices>...</vertices>
    <edges>...</edges>
</graph>

The <vertices> container will have <vertex> elements to carry the information for each vertex. In particular, you will need a unique identifier by which edges can refer to a vertex. So, something like this

    <vertices>
        <vertex id="A">other information, if needed</vertex>
        <vertex id="B">...</vertex>
        ...
    </vertices>

Each <edge> inside the <edges> container will need at least three pieces of information: the vertices connected, and the weight on the edge. Thus, for example

    <edges>
        <edge ends="A B">7</edge>
        <edge ends="B C">8</edge>
        ...
    </edges> 

You can put all this into a schema where, for example, you can enforce a requirement that the 'ends' attribute carries references to ids of vertices that do exist in the graph (viz., look up xs:ID and xs:IDREF in the XML Schema documentation).

Upvotes: 0

DanDan
DanDan

Reputation: 10562

Do you need to turn the graphic into xml, or do you have the data in some format already?

The first is hard, for the second you can take a look at Boost::Graph.

Upvotes: 0

Related Questions