java 對xml的增刪改查

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <person id="1">
  <username >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>

 

 

*****************************

package com.test;

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 = "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);
  // ...
  // }
 }

 // 在原有的xml文件中增加數據
 public static void add() throws JDOMException, FileNotFoundException,
   IOException {
  SAXBuilder builder = new SAXBuilder();
  String xmlPath = "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));
 }

 public static void edit(int id) throws JDOMException,
   FileNotFoundException, IOException {
  SAXBuilder builder = new SAXBuilder();
  Document document = builder.build("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("message.xml"));
 }
 //刪除
 public static void del(int id) throws JDOMException, FileNotFoundException,
   IOException {
  SAXBuilder builder = new SAXBuilder();
  Document document = builder.build("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("message.xml"));
 }

 public static void main(String[] args) {
  try {
   //XmlTest.edit(1);
   XmlTest.del(1);
   // XmlTest.add();
    XmlTest.list();
  } catch (JDOMException e1) {
   e1.printStackTrace();
  } catch (IOException e1) {
   e1.printStackTrace();
  }

 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章