一:要操作的xml文件     xml.xml

二:要写进xml.xml文件的节点类映射

      --MyInfo.java--

      class MyInfo
    {
        public String name;
        public String telephone;
        public String address;
        public MyInfo(){}
    }

三:追加节点

     --AppendNode.java--

     import java.io.*;
     import org.dom4j.*;
     import org.dom4j.io.*;
     class AppendNode
    {
        private String fileName;
        public AppendNode(String fileName)
       {
            this.fileName=fileName;
       }
       public void initFile()
      {
          try
           {
                org.dom4j.Element info=DocumentHelper.createElement("Info");
                org.dom4j.Document root=DocumentHelper.createDocument(info);
                org.dom4j.io.XMLWriter xmlWriter=new XMLWriter(new java.io.FileOutputStream(this.fileName));
                xmlWriter.write(root);
           }catch(java.io.FileNotFoundException e1){}
             catch(java.io.UnsupportedEncodingException e2){}
             catch(java.io.IOException e3){}
      }
      public void appendNode(MyInfo mi)
     {
          try
          {
              org.dom4j.Element myinfo=DocumentHelper.createElement("myinfo");
              org.dom4j.Element name  =DocumentHelper.createElement("name");
              org.dom4j.Element telephone=DocumentHelper.createElement("telephone");
              org.dom4j.Element address =DocumentHelper.createElement("address");
              name.setText(mi.name);
              telephone.setText(mi.telephone);
              address.setText(mi.address);
              myinfo.add(name);
              myinfo.add(telephone);
              myinfo.add(address);
              org.dom4j.io.SAXReader reader=new SAXReader();
              org.dom4j.Document root=reader.read(new java.io.FileInputStream(this.fileName));
              org.dom4j.Element  info=root.getRootElement();
              info.add(myinfo);
             org.dom4j.io.XMLResult result=new XMLResult(new java.io.FileOutputStream(this.fileName));
             org.dom4j.io.XMLWriter writer=result.getXMLWriter();
             writer.write(root);
          }catch(java.io.FileNotFoundException e4){}
            catch(org.dom4j.DocumentException e5){}
            catch(java.io.UnsupportedEncodingException e6){}
            catch(java.io.IOException e7){}
       }
    }

四:主执行类

    --XmlExec.java--

    public class XmlExec
   {
       private AppendNode appendnode;
       private String     filename;
       public XmlExec()
      {
          filename="xml.xml";  
          appendnode=new AppendNode(filename);
          appendnode.initFile();

-------------------------------------------------<1>-------------
      }
      public void xmlAppend()
     {
        MyInfo myinfo1=new MyInfo();
        MyInfo myinfo2=new MyInfo();
  
        myinfo1.name="wangkangfan";
        myinfo1.telephone="123456789";
        myinfo1.address="xiangjiang_xizihu";
  
        myinfo2.name="shenmuzi";
        myinfo2.telephone="987654321";
        myinfo2.address="diao_yu_dao";
  
       appendnode.appendNode(myinfo1);
       appendnode.appendNode(myinfo2);  
     }
     public static void main(String args[])
    {
         XmlExec xml=new XmlExec();
         xml.xmlAppend();

--------------------------------------------<2>---------------
    }
  }

五:Result view

        执行<1>:

        执行<2>: