需求:采用mxgraph实现可 拖动 拓扑图,并将移动后的拓扑图数据保存入数据库,供下次显示时读取
前台提交数据
-
- var enc1 = new mxCodec(mxUtils.createXmlDocument());
- var node1 = enc1.encode(graph.getModel());
- var xml1 = mxUtils.getXml(node1);
-
-
- TopoService.saveTopoData(xml1,function(result){
- });
后台解析数据
主要采用dom4j进行xml解析,分两套方案
-
-
-
-
-
- public int saveTopoData(String xmldata) {
- int updateResult = 1;
- Map<String, String> paraMap = new HashMap<String, String>();
- InputSource in = new InputSource(new StringReader(xmldata));
-
- in.setEncoding("GBK");
- SAXReader reader = new SAXReader();
- Document document;
- try {
- document = reader.read(in);
-
- System.out.println("===============所有需要保存的节点============");
- System.out.println("======================方案二========================");
-
- Element rootElt = document.getRootElement();
- Element rootjd = rootElt.element("root");
- Iterator rootiter = rootjd.elementIterator("mxCell");
- while (rootiter.hasNext()) {
- Element recordEle = (Element) rootiter.next();
- String autoSaveNode = recordEle.attributeValue("autoSaveNode");
- if(autoSaveNode!=null && !"".equals(autoSaveNode)){
- System.out.println("==节点允许保存:"+autoSaveNode);
- Element xyEle = recordEle.element("mxGeometry");
- System.out.println("节点id:"+recordEle.attributeValue("id"));
- System.out.println("x坐标:"+xyEle.attributeValue("x"));
- System.out.println("y坐标:"+xyEle.attributeValue("y"));
-
- String zbElementX = xyEle.attributeValue("x")==null?"0":xyEle.attributeValue("x");
- String zbElementY = xyEle.attributeValue("y")==null?"0":xyEle.attributeValue("y");
- if (zbElementX.contains(".")) {
- zbElementX = zbElementX.substring(0,zbElementX.indexOf("."));
- }
-
- if (zbElementY.contains(".")) {
- zbElementY = zbElementY.substring(0,zbElementY.indexOf("."));
- }
-
-
- paraMap.put("deviceid",Long.parseLong(recordEle.attributeValue("deviceid"))+"");
- paraMap.put("xpoint",zbElementX);
- paraMap.put("ypoint",zbElementY);
-
- topoDAO.saveTopoData(paraMap);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- } catch (Exception e) {
- e.printStackTrace();
-
- } finally {
- return updateResult;
-
- }
方案一性能比较好,因为他直接是
List<Element> eList =
document.selectNodes(xpath);//获取所有拥有autoSaveNode属性的mxCell节点
直接获取需要修改的节点,不需要就该的节点将不进行保存
问题:目前windows下正常,但linux上就出问题了,报什么:缺少jaxen.jar神马的,加入还是不行
方案二性能较差,因为他要遍历所有的xml节点,如果节点过多,性能明显会比方案一差
mxGraph实现拓扑图拖动。mxGraph提交xml数据,java后台解析
原文:http://www.cnblogs.com/CoffeeHome/p/3531256.html