方法如下:
public static void AddXML(DeptVO vo) throws Exception {
FileInputStream fi = null;
FileOutputStream fo = null;
try {
String path = "1.xml ";
fi = new FileInputStream(path);
SAXBuilder sb = new SAXBuilder();//建立构造器
Document doc = sb.build(fi);
Element root = doc.getRootElement();//得到根元素
List roots = root.getChildren();//得到根元素所有子元素的集合
int deptno = vo.getDeptno();
String dname = vo.getDname();
String loc = vo.getLoc();
Element newroot = new Element( "dept ");
Element newdeptno = new Element( "deptno ");
newdeptno.setText(Integer.toString(deptno));
newroot.addContent(newdeptno);
Element newdname = new Element( "dname ");
newdname.setText(dname);
newroot.addContent(newdname);
Element newloc = new Element( "loc ");
newloc.setText(loc);
newroot.addContent(newloc);
roots.add(newroot);//增加子元素
String indent = " ";
boolean newLines = true;
XMLOutputter out = new XMLOutputter(indent, newLines);