package jdom; import java.io.File; import java.io.IOException; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; public class JDomConvert ... { public static void main(String[] args) ...{ SAXBuilder builder=new SAXBuilder(); String realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"jdom"+File.separator+"student.xml"; try ...{ //新增节点 Document doc=builder.build(new File(realpath)); Element eleStu=new Element("student"); Element eleName=new Element("name"); Element eleAge=new Element("age"); eleName.setText("gao1"); eleAge.setText("271"); eleStu.setAttribute("sn1","011"); eleStu.addContent(eleName); eleStu.addContent(eleAge); Element root=doc.getRootElement(); root.addContent(eleStu); //删除节点 root.removeChild("student");//删除第一个student //修改节点 root.getChild("student").getChild("age").setText("22");//得到第一个student的age,并修改为22 } catch (JDOMException e) ...{ e.printStackTrace(); } catch (IOException e) ...{ e.printStackTrace(); } }}