• 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());
    		}
    	       
    	}
    
  • 相关阅读:
    OSI结构和TCP/IP模型
    将[4,3,2,5,4,3]分割成[4,3,2]、[5,4,3]两个List的算法
    Mybatis增加对象属性不增加mapper.xml的情况
    以脚本方式直接执行修改密码的passwd命令
    Raphael的鼠标over move out事件
    Raphael的Braille例子
    Raphael的set使用
    Raphael的transform用法
    Raphael的text及对齐方式
    Raphael初始化,path,circle,rect,ellipse,image
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/5304852.html
Copyright © 2020-2023  润新知