往xml文件中加入一个节点的简单实现

public class addNode {

	public static void main(String[] args) throws Exception{
		SAXReader reader = new SAXReader();
		Document doc = reader.read(new FileInputStream("student.xml"));
		Element root = doc.getRootElement();
		Element e1 = root.addElement("student");
		e1.addElement("id").addText("007");
        e1.addElement("name").addText("xiao");
        e1.addElement("email").addText("[email protected]");
        e1.addElement("gender").addText("女");
        e1.addElement("age").addText("20");
        e1.addElement("java").addText("100");
        FileOutputStream fos = new FileOutputStream("student.xml");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
        OutputFormat of = new OutputFormat();
        of.setEncoding("GBK");
        of.setIndent(true);
        //of.setIndent("    ");
        of.setNewlines(true);
        XMLWriter writer = new XMLWriter(osw, of);
        writer.write(doc);
        writer.close();
	}
}

你可能感兴趣的:(JavaEE)