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());
    		}
    	       
    	}
    
  • 相关阅读:
    递归算法
    linux下如何使用split
    什么是OPTEE-OS
    ubuntu 18.04 64bit如何编译安装内核
    ubuntu 18.04 64bit没有声音如何解决
    如何解决ubuntu报的错误:You must put some 'source' URIs in your sources.list
    linux下如何安装解压工具rar
    如何将一个已有的仓库推送到一个空的新仓库中
    ubuntu 18.04 64bit下如何安装python开发工具jupyter
    ubuntu 18.04 64bit下如何安装python开发工具jupyterhub
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/5304852.html
Copyright © 2011-2022 走看看