日期:2014-05-20 浏览次数:20700 次
//写入XML private void writeXML(Document doc) { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); try { XMLWriter writer = new XMLWriter(new FileOutputStream("C:\\info.xml"), format); writer.write(doc); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //添加管理员 public void insert(Student student) throws Exception { Document doc = this.readXML(); Element root = doc.getRootElement(); Element studentElement = root.addElement("student"); Element studentNameElement = studentElement .addElement("name"); usernameElement.addText(student.getStudentName()); Element passwordElement = studentElement .addElement("password"); passwordElement.addText(student.getPassword()); .... this.writeXML(doc); } }
------解决方案--------------------
//读取XML private Document readXML() throws Exception { SAXReader reader = new SAXReader(); Document doc = reader.read(new File"C:\\info.xml")); return doc; }
------解决方案--------------------
SAX 不能用于添加节点