需求:采用mxgraph实现可 拖动 拓扑图,并将移动后的拓扑图数据保存入数据库,供下次显示时读取
前台提交数据
- //获取mxgraph拓扑图数据
- var enc1 = new mxCodec(mxUtils.createXmlDocument());
- var node1 = enc1.encode(graph.getModel());
- var xml1 = mxUtils.getXml(node1);
- //采用dwr的ajax方式向后台提交数据
- TopoService.saveTopoData(xml1,function(result){ //保存结果
- });
后台解析数据
主要采用dom4j进行xml解析,分两套方案
- /**
- * 保存拓扑节点坐标信息
- * @param userInfo
- * @return
- */
- 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("UTF-8");
- in.setEncoding("GBK");
- SAXReader reader = new SAXReader();
- Document document;
- try {
- document = reader.read(in);
- //获取所有拥有autoSaveNode属性的mxCell节点
- System.out.println("===============所有需要保存的节点============");
- System.out.println("======================方案二========================");
- //---------------------------方案一-----------------------------------------------------
- Element rootElt = document.getRootElement(); // 获取根节点
- Element rootjd = rootElt.element("root");
- Iterator rootiter = rootjd.elementIterator("mxCell"); // 获取根节点下的子节点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);
- }
- }
- //-------------------------------------------------------------------------------------
- //---------------------------方案二-----------------------------------------------------
- /**String xpath = "//mxCell[@autoSaveNode]";
- List<Element> eList = document.selectNodes(xpath);//获取所有拥有autoSaveNode属性的mxCell节点
- for (Iterator iterator = eList.iterator(); iterator.hasNext();) {
- Element element = (Element) iterator.next();
- Element zbElement = (Element) element.elements().get(0);//坐标数据节点
- System.out.println("节点id:"+element.attributeValue("id"));
- System.out.println("x坐标:"+zbElement.attributeValue("x"));
- System.out.println("y坐标:"+zbElement.attributeValue("y"));
- String zbElementX = zbElement.attributeValue("x")==null?"0":zbElement.attributeValue("x");
- String zbElementY = zbElement.attributeValue("y")==null?"0":zbElement.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(element.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节点,如果节点过多,性能明显会比方案一差