JDOM对xml的增删改查之SAXBuilder

message.xml文件位于src目录下的xmldom包中
1.message.xml文件:

Java代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <root>   
  3.     <person id="1">   
  4.         <username>admin</username>   
  5.         <password>password1</password>   
  6.     </person>   
  7.     <person id="2">   
  8.         <username>manager</username>   
  9.         <password>password2</password>   
  10.     </person>   
  11. </root>  
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <person id="1">
      <username>admin</username>
      <password>password1</password>
    </person>
    <person id="2">
     <username>manager</username>
        <password>password2</password>
    </person>
</root>


2.查询数据

Java代码
  1. package xmldom;   
  2.   
  3. import java.io.*;   
  4. import java.util.*;   
  5. import org.jdom.*;   
  6. import org.jdom.input.SAXBuilder;   
  7. import org.jdom.output.XMLOutputter;   
  8.   
  9. /**  
  10.  * xml的增删改查之SAXBuilder  
  11.  *   
  12.  * @author Administrator  
  13.  *   
  14.  */  
  15. public class XmlTest {   
  16.   
  17.     // 查询所有的数据   
  18.     public static void list() throws JDOMException, IOException {   
  19.         SAXBuilder builder = new SAXBuilder();   
  20.         String xmlPath = "./src/xmldom/message.xml";   
  21.         //String xmlPath = "./WebRoot/xml/message.xml";   
  22.         // 获得文档对象   
  23.         Document document = builder.build(xmlPath);   
  24.         // 获得根节点   
  25.         Element root = document.getRootElement();   
  26.         List list = root.getChildren();   
  27.         System.out.println("root : " + root);   
  28.         System.out.println("root.getName : "+root.getName());   
  29.         System.out.println("listSize : "+list.size());   
  30.         Iterator it = list.iterator();   
  31.         while (it.hasNext()) {   
  32.             Element e = (Element) it.next();   
  33.             System.out.println("ID: " + e.getAttributeValue("id"));   
  34.             System.out.println("childUsername:"+e.getChildText("username"));   
  35.             System.out.println("childPassword:"+e.getChildText("password"));   
  36.         }   
  37.            
  38.         //for(int i=0;i<list.size();i++){   
  39.         //  Element e = (Element)list.get(i);   
  40.         //  ...   
  41.         //}   
  42.     }   
  43.     public static void main(String[] args) {   
  44.         try {   
  45.             XmlTest.list();   
  46.         } catch (JDOMException e1) {   
  47.             e1.printStackTrace();   
  48.         } catch (IOException e1) {   
  49.             e1.printStackTrace();   
  50.         }   
  51.     }   
  52. }  
package xmldom;

import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**
 * xml的增删改查之SAXBuilder
 * 
 * @author Administrator
 * 
 */
public class XmlTest {

 // 查询所有的数据
 public static void list() throws JDOMException, IOException {
  SAXBuilder builder = new SAXBuilder();
  String xmlPath = "./src/xmldom/message.xml";
  //String xmlPath = "./WebRoot/xml/message.xml";
  // 获得文档对象
  Document document = builder.build(xmlPath);
  // 获得根节点
  Element root = document.getRootElement();
  List list = root.getChildren();
  System.out.println("root : " + root);
  System.out.println("root.getName : "+root.getName());
  System.out.println("listSize : "+list.size());
  Iterator it = list.iterator();
  while (it.hasNext()) {
   Element e = (Element) it.next();
   System.out.println("ID: " + e.getAttributeValue("id"));
   System.out.println("childUsername:"+e.getChildText("username"));
   System.out.println("childPassword:"+e.getChildText("password"));
  }
  
  //for(int i=0;i<list.size();i++){
  // Element e = (Element)list.get(i);
  // ...
  //}
 }
 public static void main(String[] args) {
  try {
   XmlTest.list();
  } catch (JDOMException e1) {
   e1.printStackTrace();
  } catch (IOException e1) {
   e1.printStackTrace();
  }
 }
}


结果:

Java代码
  1. root : [Element: <root/>]   
  2. root.getName : root   
  3. listSize : 2  
  4. ID: 1  
  5. childUsername:admin   
  6. childPassword:password1   
  7. ID: 2  
  8. childUsername:manager   
  9. childPassword:password2  
root : [Element: <root/>]
root.getName : root
listSize : 2
ID: 1
childUsername:admin
childPassword:password1
ID: 2
childUsername:manager
childPassword:password2


3.在原有的xml文件中增加数据

Java代码
  1. public static void add() throws JDOMException, FileNotFoundException,IOException {   
  2.     SAXBuilder builder = new SAXBuilder();   
  3.     String xmlPath = "./src/xmldom/message.xml";   
  4.     Document document = builder.build(xmlPath);   
  5.        
  6.     //获得根节点   
  7.     Element root = document.getRootElement();   
  8.        
  9.     //创建节点person   
  10.     Element element = new Element("person");   
  11.        
  12.     //给person节点添加属性id   
  13.     element.setAttribute("id""3");   
  14.        
  15.     //给person节点添加子节点并赋值   
  16.     element.addContent(new Element("username").setText("hello"));   
  17.     element.addContent(new Element("password").setText("woerld"));   
  18.        
  19.     //给父节点添加person子节点   
  20.     root.addContent(element);   
  21.   
  22.     XMLOutputter output = new XMLOutputter();   
  23.     output.output(document, new FileOutputStream(xmlPath));   
  24. }   
  25.    
 public static void add() throws JDOMException, FileNotFoundException,IOException {
  SAXBuilder builder = new SAXBuilder();
  String xmlPath = "./src/xmldom/message.xml";
  Document document = builder.build(xmlPath);
  
  //获得根节点
  Element root = document.getRootElement();
  
  //创建节点person
  Element element = new Element("person");
  
  //给person节点添加属性id
  element.setAttribute("id", "3");
  
  //给person节点添加子节点并赋值
  element.addContent(new Element("username").setText("hello"));
  element.addContent(new Element("password").setText("woerld"));
  
  //给父节点添加person子节点
  root.addContent(element);

  XMLOutputter output = new XMLOutputter();
  output.output(document, new FileOutputStream(xmlPath));
 }
  


测试:

Java代码
  1. XmlTest.add();  
XmlTest.add();


结果生成新的xml文件:

Java代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <root>   
  3.     <person id="1">   
  4.         <username>admin</username>   
  5.         <password>password1</password>   
  6.     </person>   
  7.     <person id="2">   
  8.         <username>manager</username>   
  9.         <password>password2</password>   
  10.     </person>   
  11.     <person id="3">   
  12.         <username>hello</username>   
  13.         <password>woerld</password>   
  14.     </person>   
  15. </root>  
<?xml version="1.0" encoding="UTF-8"?>
<root>
 <person id="1">
  <username>admin</username>
  <password>password1</password>
 </person>
 <person id="2">
  <username>manager</username>
  <password>password2</password>
 </person>
 <person id="3">
  <username>hello</username>
  <password>woerld</password>
 </person>
</root>


4.修改xml中数据
a.修改前xml文件:

Java代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <root>   
  3.     <person id="1">   
  4.         <username ip="1111111">admin</username>   
  5.         <password>admin</password>   
  6.     </person>   
  7.     <person id="2">   
  8.         <username>manager</username>   
  9.         <password>password2</password>   
  10.     </person>   
  11.     <person id="3">   
  12.         <username>hello</username>   
  13.         <password>woerld</password>   
  14.     </person>   
  15. </root>  
<?xml version="1.0" encoding="UTF-8"?>
<root>
 <person id="1">
  <username ip="1111111">admin</username>
  <password>admin</password>
 </person>
 <person id="2">
  <username>manager</username>
  <password>password2</password>
 </person>
 <person id="3">
  <username>hello</username>
  <password>woerld</password>
 </person>
</root>


b.调用方法:

Java代码
  1. public static void edit(int id) throws JDOMException,   
  2.             FileNotFoundException, IOException {   
  3.         SAXBuilder builder = new SAXBuilder();   
  4.         Document document = builder.build("./src/xmldom/message.xml");   
  5.         Element root = document.getRootElement();   
  6.   
  7.         List list = root.getChildren();   
  8.         Iterator it = list.iterator();   
  9.         for (int i = 0; i < list.size(); i++) {   
  10.             Element e = (Element) it.next();   
  11.             System.out.println("==============" + e.getAttributeValue("id"));   
  12.             if (Integer.parseInt(e.getAttributeValue("id")) == id) {   
  13.                 e.getChild("username").setText("xiaoma");   
  14.                 e.getChild("username").setAttribute("ip""66666666666");   
  15.                 e.getChild("password").setText("xiaoma");   
  16.   
  17.             }   
  18.         }   
  19.         XMLOutputter output = new XMLOutputter();   
  20.         output.output(document,   
  21.                 new FileOutputStream("./src/xmldom/message.xml"));   
  22.     }  
public static void edit(int id) throws JDOMException,
   FileNotFoundException, IOException {
  SAXBuilder builder = new SAXBuilder();
  Document document = builder.build("./src/xmldom/message.xml");
  Element root = document.getRootElement();

  List list = root.getChildren();
  Iterator it = list.iterator();
  for (int i = 0; i < list.size(); i++) {
   Element e = (Element) it.next();
   System.out.println("==============" + e.getAttributeValue("id"));
   if (Integer.parseInt(e.getAttributeValue("id")) == id) {
    e.getChild("username").setText("xiaoma");
    e.getChild("username").setAttribute("ip", "66666666666");
    e.getChild("password").setText("xiaoma");

   }
  }
  XMLOutputter output = new XMLOutputter();
  output.output(document,
    new FileOutputStream("./src/xmldom/message.xml"));
 }


c.测试:

Java代码
  1. XmlTest.edit(1);  
XmlTest.edit(1);


d.执行后的xml文件:

Java代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <root>   
  3.     <person id="1">   
  4.         <username ip="66666666666">xiaoma</username>   
  5.         <password>xiaoma</password>   
  6.     </person>   
  7.     <person id="2">   
  8.         <username>manager</username>   
  9.         <password>password2</password>   
  10.     </person>   
  11.     <person id="3">   
  12.         <username>hello</username>   
  13.         <password>woerld</password>   
  14.     </person>   
  15. </root>  
<?xml version="1.0" encoding="UTF-8"?>
<root>
 <person id="1">
  <username ip="66666666666">xiaoma</username>
  <password>xiaoma</password>
 </person>
 <person id="2">
  <username>manager</username>
  <password>password2</password>
 </person>
 <person id="3">
  <username>hello</username>
  <password>woerld</password>
 </person>
</root>


5.删除xml中记录
a.删除前xml文件如上
b.执行方法:

Java代码
  1. public static void del(int id) throws JDOMException, FileNotFoundException,   
  2.             IOException {   
  3.         SAXBuilder builder = new SAXBuilder();   
  4.         Document document = builder.build("./src/xmldom/message.xml");   
  5.         Element root = document.getRootElement();   
  6.   
  7.         List list = root.getChildren();   
  8.         Iterator it = list.iterator();   
  9.   
  10.         for (int i = 0; i < list.size(); i++) {   
  11.             Element e = (Element) it.next();   
  12.             if (Integer.parseInt(e.getAttributeValue("id")) == id) {   
  13.                 root.removeContent(e);   
  14.                 break;   
  15.             }   
  16.         }   
  17.         // 文件处理   
  18.         XMLOutputter out = new XMLOutputter();   
  19.         out.output(document, new FileOutputStream("./src/xmldom/message.xml"));   
  20.     }  
public static void del(int id) throws JDOMException, FileNotFoundException,
   IOException {
  SAXBuilder builder = new SAXBuilder();
  Document document = builder.build("./src/xmldom/message.xml");
  Element root = document.getRootElement();

  List list = root.getChildren();
  Iterator it = list.iterator();

  for (int i = 0; i < list.size(); i++) {
   Element e = (Element) it.next();
   if (Integer.parseInt(e.getAttributeValue("id")) == id) {
    root.removeContent(e);
    break;
   }
  }
  // 文件处理
  XMLOutputter out = new XMLOutputter();
  out.output(document, new FileOutputStream("./src/xmldom/message.xml"));
 }


c.测试:

Java代码
  1. XmlTest.del(3);  
XmlTest.del(3);


d.执行后xml文件:

Java代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <root>   
  3.     <person id="1">   
  4.         <username ip="66666666666">xiaoma</username>   
  5.         <password>xiaoma</password>   
  6.     </person>   
  7.     <person id="2">   
  8.         <username>manager</username>   
  9.         <password>password2</password>   
  10.     </person>   
  11.        
  12. </root>  

你可能感兴趣的:(xml)