java修改XML文件节点的属性输出文件问题
输出文件时,属性的值没有发生改变
?
?
try {String src = request.getSession().getServletContext().getRealPath("/");String path=src + "xml/scene.xml";File file = new File(src + "xml/scene.xml");SAXReader sax = new SAXReader();Document document = sax.read(file);Element root = document.getRootElement();Element returnHome = root.element("returnHome");Element lightin = returnHome.element("lighting");System.out.println(lightin);//String[] lighting = request.getParameterValues("lighting");//String[] curtain = request.getParameterValues("curtain");boolean flag=false;for (Iterator it = lightin.elementIterator(); it.hasNext();) {Element element = (Element) it.next();System.out.println(element.attribute("value").getValue());System.out.println(element.attribute("status").getValue());if ("lighiting1".equals(element.attributeValue("value"))) {element.attribute("status").setValue("1");element.attribute("name").setValue("XXXXXXXXXXX");flag=true;//closeAction();continue;}}if (flag) {FileWriter writer = new FileWriter(path); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("utf-8"); XMLWriter xmlwriter = new XMLWriter(writer, format); xmlwriter.write(document); writer.flush(); xmlwriter.flush(); writer.close(); xmlwriter.close();// FileWriter out = new FileWriter(filename);// document.write(out);// out.flush();// out.close();}} catch (DocumentException e) {e.printStackTrace();}
?
原因:
?? 在输入新文件的时候,应该指定文件的路径.