日期:2014-05-19 浏览次数:20692 次
/** * 修改XMl文件指定节点的值 * * @param xmlPath * Xml文件路径 * @param CNode * 判断节点 * @param CValue * 判断节点值 * @param Node * 修改节点 * @param newValue * 修改值 */ public boolean modifyXml(String xmlPath, String CNode, String CValue, String Node, String newValue) { SAXReader reader = new SAXReader(); boolean flag = false; try { Document doc = reader.read(this.getClass().getClassLoader() .getResourceAsStream(xmlPath)); Element root = doc.getRootElement(); List<Element> nodes = root.elements(); for (Element node : nodes) { List<Element> nodes2 = node.elements(); for (Element node2 : nodes2) { if (node2.getName().equals(CNode) && node2.getData().toString().equals(CValue)) { for (Element node3 : nodes2) { if (node3.getName().equals(Node)) { node3.setText(newValue); flag = true; break; } } break; } } } FileWriter fw = new FileWriter(this.getClass().getClassLoader() .getResource(xmlPath).getPath()); XMLWriter writer = new XMLWriter(fw); writer.write(doc); fw.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } return flag; }
modifyXml("MessageUsers.xml", "name", name,"todayLastSendTime",todayLastSendTimeStr); modifyXml("MessageUsers.xml", "name", name,"todaySendCount", todaySendCount + "");