采用JDom实现Xml文档增加,删除,查询,修改。

 

 
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <people> 
  3.   <!--将数据从程序输出到XML中!--> 
  4.   <person id="002" gender="male"> 
  5.     <name>佳佳</name> 
  6.     <address>安徽</address> 
  7.   </person> 
  8.   <person id="003" gender="male"> 
  9.     <name>王力宏</name> 
  10.     <address>中国</address> 
  11.   </person> 
  12. </people> 
  13.  

   采用如下代码进行操作:

 

 
  
  1. import java.io.File;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.util.Iterator;  
  6. import java.util.List;  
  7.  
  8. import org.jdom.Attribute;  
  9. import org.jdom.Comment;  
  10. import org.jdom.Document;  
  11. import org.jdom.Element;  
  12. import org.jdom.JDOMException;  
  13. import org.jdom.input.SAXBuilder;  
  14. import org.jdom.output.Format;  
  15. import org.jdom.output.XMLOutputter;  
  16.  
  17. public class JdomTest {  
  18.  
  19.     public static void main(String[] args) { // 创建文档  
  20.  
  21.         try {  
  22.             // 初始化  
  23.             JdomTest.init();  
  24.             // 添加一个节点  
  25.             JdomTest.add();  
  26.             // 遍历xml  
  27.             JdomTest.traverse();  
  28.             // 删除id为1的节点  
  29.             JdomTest.del(001);  
  30.             // 编辑id为2的节点  
  31.             JdomTest.edit(002);  
  32.         } catch (JDOMException e) {  
  33.  
  34.             e.printStackTrace();  
  35.         } catch (IOException e) {  
  36.  
  37.             e.printStackTrace();  
  38.         }  
  39.  
  40.     }  
  41.  
  42.     private static void edit(int i) throws JDOMException, IOException {  
  43.         SAXBuilder builder = new SAXBuilder();  
  44.         String path = "jdom.xml";  
  45.         Document document = builder.build(new File(path));  
  46.         Element root = document.getRootElement();  
  47.         @SuppressWarnings("unchecked")  
  48.         List<Element> list = root.getChildren();  
  49.         Iterator<Element> it = list.iterator();  
  50.         while (it.hasNext()) {  
  51.             Element e = it.next();  
  52.             if (Integer.parseInt(e.getAttributeValue("id")) == i) {  
  53.                 e.getChild("name").setText(“佳佳");  
  54.                 e.getChild("address").setText("安徽");  
  55.             }  
  56.         }  
  57.  
  58.         XMLOutputter out = new XMLOutputter();  
  59.         out.setFormat(out.getFormat().setEncoding("utf-8").setIndent("  "));  
  60.         out.output(document, new FileOutputStream(path));  
  61.     }  
  62.  
  63.     private static void del(int i) throws JDOMException, IOException {  
  64.         SAXBuilder builder = new SAXBuilder();  
  65.         String path = "jdom.xml";  
  66.         Document document = builder.build(new File(path));  
  67.         Element root = document.getRootElement();  
  68.         @SuppressWarnings("unchecked")  
  69.         List<Element> list = root.getChildren();  
  70.  
  71.         Iterator<Element> it = list.iterator();  
  72.  
  73.         while (it.hasNext()) {  
  74.             Element e = it.next();  
  75.             if (Integer.parseInt(e.getAttributeValue("id")) == i) {  
  76.                 root.removeContent(e);  
  77.                 break;  
  78.             }  
  79.         }  
  80.  
  81.         XMLOutputter out = new XMLOutputter();  
  82.         out.setFormat(out.getFormat().setEncoding("utf-8").setIndent("  "));  
  83.         out.output(document, new FileOutputStream(path));  
  84.     }  
  85.  
  86.     private static void traverse() throws JDOMException, IOException {  
  87.         SAXBuilder builder = new SAXBuilder();  
  88.         String path = "jdom.xml";  
  89.         Document document = builder.build(new File(path));  
  90.         Element root = document.getRootElement();  
  91.         @SuppressWarnings("unchecked")  
  92.         List<Element> list = root.getChildren();  
  93.  
  94.         Iterator<Element> it = list.iterator();  
  95.         while (it.hasNext()) {  
  96.             Element e = it.next();  
  97.             System.out.println("编号:" + e.getAttributeValue("id") + "性别:" 
  98.                     + e.getAttributeValue("gender") + "姓名:" 
  99.                     + e.getChild("name").getText() + "地址:" 
  100.                     + e.getChild("address").getText());  
  101.         }  
  102.  
  103.     }  
  104.  
  105.     private static void add() throws JDOMException, IOException {  
  106.  
  107.         SAXBuilder builder = new SAXBuilder();  
  108.         String path = "jdom.xml";  
  109.         Document document = builder.build(new File(path));  
  110.         Element root = document.getRootElement();  
  111.  
  112.         Element person = new Element("person");  
  113.         person.setAttribute("id""003").setAttribute("gender""male");  
  114.         root.addContent(person);  
  115.  
  116.         Element name = new Element("name");  
  117.         name.setText("王力宏");  
  118.         person.addContent(name);  
  119.  
  120.         Element address = new Element("address");  
  121.         address.setText("中国");  
  122.         person.addContent(address);  
  123.  
  124.           
  125.           
  126.           
  127.         XMLOutputter out = new XMLOutputter();  
  128.  
  129.         out.setFormat(out.getFormat().setEncoding("utf-8").setIndent("    "));  
  130.         out.output(document, new FileOutputStream(new File(path)));  
  131.  
  132.     }  
  133.  
  134.     private static void init() throws FileNotFoundException, IOException {  
  135.         // 创建文档  
  136.         Document document = new Document();  
  137.         // 创建根元素  
  138.         Element people = new Element("people");  
  139.         // 把根元素加入到document中  
  140.         document.addContent(people);  
  141.         // 创建注释  
  142.         Comment rootComment = new Comment("将数据从程序输出到XML中!");  
  143.         people.addContent(rootComment);  
  144.         // 创建父元素  
  145.         Element person1 = new Element("person");  
  146.         // 把元素加入到根元素中  
  147.         people.addContent(person1);  
  148.         // 设置person1元素属性  
  149.         person1.setAttribute("id""001");  
  150.         Attribute person1_gender = new Attribute("gender""male");  
  151.         person1.setAttribute(person1_gender);  
  152.         // 设置person1的姓名 并将其添加到 person1  
  153.         Element person1_name = new Element("name");  
  154.         person1_name.setText("刘德华");  
  155.         person1.addContent(person1_name);  
  156.         // 设置person1的地址 并将其添加到person1  
  157.         Element person1_address = new Element("address");  
  158.         person1_address.setText("香港");  
  159.         person1.addContent(person1_address);  
  160.         // 创建元素person2  
  161.         Element person2 = new Element("person");  
  162.         people.addContent(person2);  
  163.         // 添加属性,可以一次添加多个属性  
  164.         person2.setAttribute("id""002").setAttribute("gender""male");  
  165.         // 设置person2的姓名 并将其添加到 person2  
  166.         Element person2_name = new Element("name");  
  167.         person2_name.setText("林志颖");  
  168.         person2.addContent(person2_name);  
  169.         // 设置person2的地址 并将其添加到person2  
  170.         Element person2_address = new Element("address");  
  171.         person2_address.setText("台湾");  
  172.         person2.addContent(person2_address);  
  173.         // 设置xml输出格式  
  174.         Format format = Format.getPrettyFormat();  
  175.         format.setEncoding("utf-8");// 设置编码  
  176.         format.setIndent("    ");// 设置缩进  
  177.         // 得到xml输出流  
  178.         XMLOutputter out = new XMLOutputter(format);  
  179.         // 把数据输出到xml中  
  180.         out.output(document, new FileOutputStream("jdom.xml"));  
  181.         // 或者FileWriter  
  182.     }  
  183. }  

  结果: