通过JGraphX API进行图形化的XML [英] XML to graph by JGraphX API

查看:327
本文介绍了通过JGraphX API进行图形化的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Java将XML解析为JGraphX中的图形.我从JGraphX库获得此XML,并且需要再次通过此XML设置图形.是否有将XML转换为图形的JGraphX库方法?我有下面的这段代码,用于从图形中获取XML:

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

那么有什么方法可以从该XML获取图形?否则,我该怎么做才能生成图形?如果尝试通过逐一读取XML来生成图形,那么使用复杂的算法可能会花费一些时间,因此我尝试查找其他库方法.

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.

推荐答案

这应将其从指定的路径读入新的mxGraph

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

这篇关于通过JGraphX API进行图形化的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆