zoukankan      html  css  js  c++  java
  • dom4j修改xml文件

    @SuppressWarnings("unchecked")
    	@Override
    	public void insertCustomInfo(File file, String modelCode,List<Property> props) {
    		SAXReader saxReadr = new SAXReader();// 得到SAXReader对象
    		try {
    			org.dom4j.Document  doc=saxReadr.read(file);
    			org.dom4j.Element root = doc.getRootElement();// 得到根节点
    			if (null != props && props.size() > 0) {
    				//初始化删除添加的节点
    				org.dom4j.Element styleElement = root.element("Styles");
    				/*Iterator<org.dom4j.Element> it =styleElement.elementIterator();
    				while(it.hasNext()) {
    					org.dom4j.Element e = it.next();
    			        if(e.)
    
    			   }*/
    				//List<org.dom4j.Element>  domList=styleElement.selectNodes("/.");
    				//开始增加数据
    				if (null != styleElement) {
    					styleElement.addComment("custome style  start");
    					for (Property p : props) {
    						//FIXME 根据属性样式不同,添加不同的样式
    						org.dom4j.Element se=styleElement.addElement("Style");
    						se.addAttribute("ss:ID", p.getCode());
    						se.addElement("NumberFormat");
    						se.addElement("Protection");
    					}
    					styleElement.addComment("custome style  end");
    				}
    				org.dom4j.Element workSheetElement = root.element("Worksheet");
    				if (null != workSheetElement) {
    					org.dom4j.Element tableElement = workSheetElement.element("Table");
    					if (null != tableElement) {
    						org.dom4j.Element rowElement = tableElement.element("Row");
    						if (null != rowElement) {
    							for (Property p : props) {
    								org.dom4j.Element cellEle=rowElement.addElement("Cell");
    								cellEle.addElement("Data").addAttribute("ss:Type", "String").addText(InternationalResource.get(p.getDisplayName(), getCurrentLanguage()));
    								org.dom4j.Element commentEle=cellEle.addElement("Comment").addAttribute("ss:Author", "");
    								//commentEle.add(attribute);
    							}
    						}
    						List<org.dom4j.Element> columnElements=tableElement.elements("Column");
    						int size=columnElements.size();
    						if(null!=columnElements&&columnElements.size()>0)  {
    							org.dom4j.Element colE = tableElement.element("Column");
    							if (null != colE) {
    								for (Property p : props) {
    									colE.addElement("Column").addAttribute("ss:Index", String.valueOf(size++));
    									colE.addElement("Column").addAttribute("ss:Index", String.valueOf(size++));
    								}
    							}
    						}
    					}
    				}
    
    			}
    			OutputFormat format = OutputFormat.createPrettyPrint();
    			try {
    				XMLWriter writer = new XMLWriter(new FileWriter(file), format);
    				writer.write(doc);
    				writer.flush();
    				writer.close();
    			} catch (UnsupportedEncodingException e) {
    				e.printStackTrace();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			log.info(root.getName());
    		} catch (DocumentException e) {
    			log.warn(e.getMessage());
    		}
    	       
    	}
    
  • 相关阅读:
    一篇文章让你了解GC垃圾回收器
    使用SpringBoot整合ssm项目
    SpringBoot项目集成Hystrix
    50个简单易懂的经济学定律
    使用POI导出EXCEL工具类并解决导出数据量大的问题
    数据库事务的四大特性以及四种隔离级别
    简单了解Redis
    如何更规范化的编写JAVA 代码
    如何在Linux服务器上部署Mysql
    常见的数据库函数,关键字整理
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/5304852.html
Copyright © 2011-2022 走看看