/** * 生成sel的document * * @param normalName normal普通状态的图片名 * @param specialName 特殊状态(pressed按下/checked选中)的图片名 * @param end 特殊状态(pressed按下/checked选中)后缀名 * @return */ public static Document createSelector(String normalName, String specialName, String end) { Document doc = XmlUtil.read("res\\drawable\\sel.xml"); Element rootElement = doc.getRootElement(); List<Element> elements = XmlUtil.getAllElements(doc); for (Element element : elements) { Attribute attr = element.attribute("drawable"); if (attr == null) { continue; } String value = attr.getStringValue(); if (value.contains(end)) { // 替换特殊状态(pressed/checked)的item加后缀 value = value.replace(end, specialName); attr.setValue(value); } else if (element.attributeCount() > 1) { // 移除不需要的element rootElement.remove(element); } else { // normal状态的item不加后缀 value = value.replace("normal", normalName); attr.setValue(value); } } return doc; }
/** * 修改xml某节点的值 * * @param inputXml 原xml文件 * @param nodes 要修改的节点 * @param attributename 属性名称 * @param value 新值 * @param outXml 输出文件路径及文件名 如果输出文件为null,则默认为原xml文件 */ public static void modifyDocument( File inputXml, String nodes, String attributename, String value, String outXml) { try { SAXReader saxReader = new SAXReader(); Document document = saxReader.read(inputXml); List list = document.selectNodes(nodes); Iterator iter = list.iterator(); while (iter.hasNext()) { Attribute attribute = (Attribute) iter.next(); if (attribute.getName().equals(attributename)) attribute.setValue(value); } XMLWriter output; if (outXml != null) { // 指定输出文件 output = new XMLWriter(new FileWriter(new File(outXml))); } else { // 输出文件为原文件 output = new XMLWriter(new FileWriter(inputXml)); } output.write(document); output.close(); } catch (DocumentException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } }
// 修改:属性值,文本 public static void main(String[] args) throws Exception { Document doc = new SAXReader().read(new File("C:/Users/Administrator/Desktop/contact.xml")); // 方案一: 修改属性值 1.得到标签对象 2.得到属性对象 3.修改属性值 // 1.1 得到标签对象 Element contactElem = doc.getRootElement().element("contact"); // 1.2 得到属性对象 Attribute idAttr = contactElem.attribute("id"); // 1.3 修改属性值 idAttr.setValue("003"); // 方案二: 修改属性值 // 1.1 得到标签对象 Element contactElem1 = doc.getRootElement().element("contact"); // 1.2 通过增加同名属性的方法,修改属性值 contactElem.addAttribute("id", "004"); // 修改文本 1.得到标签对象 2.修改文本 Element nameElem = doc.getRootElement().element("contact").element("name"); nameElem.setText("demo7"); FileOutputStream out = new FileOutputStream("C:/Users/Administrator/Desktop/test/contact.xml"); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter writer = new XMLWriter(out, format); writer.write(doc); writer.close(); }
/** * 修改XML文件中内容,并另存为一个新文件 重点掌握dom4j中如何添加节点,修改节点,删除节点 * * @param filename 修改对象文件 * @param newfilename 修改后另存为该文件 * @return 返回操作结果, 0表失败, 1表成功 */ public int modifyXMLFile(String filename, String newfilename) { int returnValue = 0; SAXReader saxReader = new SAXReader(); Document doc = null; try { /** 修改内容之一:如果book节点中show参数的内容为yes,则修改成no */ /** 先用xpath查找对象 */ doc = saxReader.read(new File(filename)); List list = doc.selectNodes("/books/book/@show"); Iterator iter = list.iterator(); while (iter.hasNext()) { Attribute attr = (Attribute) iter.next(); if ("yes".equals(attr.getValue())) { attr.setValue("no"); } } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } /** 修改内容之二:把owner项内容改为Tshinghua 并在owner节点中加入date节点,date节点的内容为2004-09-11,还为date节点添加一个参数type */ List list = doc.selectNodes("/books/owner"); Iterator iter = list.iterator(); if (iter.hasNext()) { Element ownEle = (Element) iter.next(); ownEle.setText("Tshinghua"); Element dateEle = ownEle.addElement("date"); dateEle.setText("2012-12-17"); dateEle.addAttribute("type", "Gregorian calendar"); } /** 修改内容之三:若title内容为Dom4j Tutorials,则删除该节点 */ List list2 = doc.selectNodes("/books/book"); Iterator iter2 = list2.iterator(); while (iter2.hasNext()) { Element bookEle = (Element) iter2.next(); Iterator iterator = bookEle.elementIterator("title"); while (iterator.hasNext()) { Element titleElement = (Element) iterator.next(); if (titleElement.getText().equals("Dom4j Tutorials")) { bookEle.remove(titleElement); } } } try { /** 将document中的内容写入文件中 */ XMLWriter writer = new XMLWriter(new FileWriter(new File(newfilename))); writer.write(doc); writer.close(); /** 执行成功,需返回1 */ returnValue = 1; } catch (Exception ex) { ex.printStackTrace(); } return returnValue; }
/** * 设置系统设置 * * @param setting 系统设置 */ public static void set(Setting setting) { try { File shopxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile(); Document document = new SAXReader().read(shopxxXmlFile); List<Element> elements = document.selectNodes("/shopxx/setting"); for (Element element : elements) { try { String name = element.attributeValue("name"); String value = beanUtils.getProperty(setting, name); Attribute attribute = element.attribute("value"); attribute.setValue(value); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } FileOutputStream fileOutputStream = null; XMLWriter xmlWriter = null; try { OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setEncoding("UTF-8"); outputFormat.setIndent(true); outputFormat.setIndent(" "); outputFormat.setNewlines(true); fileOutputStream = new FileOutputStream(shopxxXmlFile); xmlWriter = new XMLWriter(fileOutputStream, outputFormat); xmlWriter.write(document); } catch (Exception e) { e.printStackTrace(); } finally { if (xmlWriter != null) { try { xmlWriter.close(); } catch (IOException e) { } } IOUtils.closeQuietly(fileOutputStream); } Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME); cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting)); } catch (Exception e) { e.printStackTrace(); } }
/** * 修改指定节点的属性值; * * @param inputXml xml文件流 * @param XPath 要修改节点属性的表达式;如:"//article/@level" 则表示修改节点level(父节点为article)的属性 * @param attributeValue 属性新值; */ @SuppressWarnings("unchecked") public Document modifyXMLNodeAttributeByName(File inputXml, String XPath, String attributeValue) { if (XPath.indexOf("@") < 0) { System.out.println("参数XPath无效,请在要修改的属性前加入'@'"); return null; } SAXReader saxReader = new SAXReader(); Document document = null; try { document = saxReader.read(inputXml); List list = document.selectNodes(XPath); Iterator iter = list.iterator(); while (iter.hasNext()) { Attribute attribute = (Attribute) iter.next(); // 把原属性修改为新的属性; attribute.setValue(attributeValue); } } catch (DocumentException e) { e.printStackTrace(); } return document; }
/** * 修改XML文件中内容,并另存为一个新文件 重点掌握dom4j中如何添加节点,修改节点,删除节点 * * @param filename 修改对象文件 * @param newfilename 修改后另存为该文件 * @return 返回操作结果, 0表失败, 1表成功 */ public int modiXMLFile(String filename, String newfilename) { int returnValue = 0; try { SAXReader saxReader = new SAXReader(); Document document = saxReader.read(new java.io.File(filename)); /** 修改内容之一: 如果book节点中show属性的内容为yes,则修改成no */ /** 先用xpath查找对象 */ List list = document.selectNodes("/books/book/@show"); Iterator iter = list.iterator(); while (iter.hasNext()) { Attribute attribute = (Attribute) iter.next(); if (attribute.getValue().equals("yes")) { attribute.setValue("no"); } } /** 修改内容之二: 把owner项内容改为"测试修改" 并在owner节点中加入date节点,date节点的内容为2004-09-11,还为date节点添加一个属性type */ list = document.selectNodes("/books/owner"); iter = list.iterator(); if (iter.hasNext()) { Element ownerElement = (Element) iter.next(); ownerElement.setText("测试修改"); Element dateElement = ownerElement.addElement("date"); dateElement.setText("2008-09-11"); dateElement.addAttribute("type", "日期"); } /** 修改内容之三: 若title内容为Dom4j Tutorials,则删除该节点 */ list = document.selectNodes("/books/book"); iter = list.iterator(); while (iter.hasNext()) { Element bookElement = (Element) iter.next(); Iterator iterator = bookElement.elementIterator("title"); while (iterator.hasNext()) { Element titleElement = (Element) iterator.next(); if (titleElement.getText().equals("Dom4j Tutorials")) { bookElement.remove(titleElement); } } } try { /** 格式化输出,类型IE浏览一样 */ OutputFormat format = OutputFormat.createPrettyPrint(); /** 指定XML编码 */ // format.setEncoding("GBK"); /** 将document中的内容写入文件中 */ // XMLWriter writer = new XMLWriter(new FileWriter(new // File(newfilename)),format); // 保证编码为UTF-8,支持中文写入 XMLWriter writer = new XMLWriter(new FileOutputStream(new File(newfilename)), format); writer.write(document); writer.close(); /** 执行成功,需返回1 */ returnValue = 1; } catch (Exception ex) { ex.printStackTrace(); } } catch (Exception ex) { ex.printStackTrace(); } return returnValue; }