XML to graph by JGraphX API
Asked Answered
A

1

2

I need to parse XML to a graph in JGraphX using Java. I get this XML from the JGraphX library and need to again set a graph by this XML. Is there any JGraphX library method that converts XML to a graph? I have this this below code to get XML from a graph:

try
{
    System.out.println("call xml getting code");
    mxCodec codec = new mxCodec();
    String xml = mxUtils.getXml(codec.encode(graph1.getModel()));
    java.io.FileWriter fw = new java.io.FileWriter("E:\\my-file.xml");
    fw.write(xml);
    fw.close();
}
catch(Exception ex)
{
    System.out.println("ERROR : "+ex.getMessage());
}  

So is there any way to get a graph from this XML? Otherwise what should I do to generate the graph? If I try to generate a graph by reading the XML one by one it may take time with complex algorithms hence I tried to find other library method.

Abeokuta answered 4/11, 2015 at 10:25 Comment(2)
are you trying to visualize data in XML ? I think you need to write your own algorithm to implement this.You got to parse XML file and add element as node object as the way you can visualize in graph api.Please show me Your XML file.Mountie
Hi, I think you described what I want to do, but do you know any good parsers for this? It should not only parse the hierarchy, but also the references.Encouragement
I
2

This should read it from the specified Path into a new mxGraph

mxGraph graph = new mxGraph();        
try
{
    Document document = mxXmlUtils.parseXml(mxUtils.readFile(filePath));
    mxCodec codec = new mxCodec(document);
    codec.decode(document.getDocumentElement(), graph.getModel());
}
catch (Exception ex)
{
    ex.printStackTrace();
}
Intramural answered 4/11, 2015 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.