Adding XML attributes to JUNG GraphML

196 views Asked by At

I'm using JUNG to construct a Graph and then writing out to GraphML using a GraphMLWriter. Using the addVertexData and addEdgeData methods I can get <data> tags output to the XML.

e.g. I can get the following:

<edge id="123" source="456" target="789">
    <data key="firstname">John</data>
</edge>

by adding a Transformer using addEdgeData

Is there a way for me to add XML attributes onto the edge node itself, e.g. to get the following?

<edge id="123" source="456" target="789" label="theDescriptionOfMyEdge">
    <data key="firstname">John</data>
</edge>
1

There are 1 answers

1
Borstel On BEST ANSWER

According to the source (methods writeVertexData() on line 151 and writeEdgeData() on line 190), this doesn't seem to be possible with the standard GraphMLWriter. One option is to subclass the implementation in the JUNG library and override these methods to include the functionality you need. However, this would possibly require duplicating a lot of code and may not be upgrade-safe if certain internals of the library class are changed.

The cleanest solution would be to implement your own well-designed writer class that enables a more flexible output, e.g. by using strategies for writing the vertices and edges. In addition, using an API such as StAX should make the writer more robust than the library implementation (which, for example, doesn't seem to perform proper XML escaping).