XML to graph by JGraphX API

試著忘記壹切 提交于 2019-12-11 00:15:25

问题


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.


回答1:


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();
}


来源:https://stackoverflow.com/questions/33519362/xml-to-graph-by-jgraphx-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!