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()); } }