论坛首页 入门技术论坛

JDOM 对Xml文件(增、删、改、查)

浏览 9914 次
该帖已经被评为新手帖
作者 正文
   发表时间:2008-08-28  

package bean;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**
 * 邵波 QQ:343269876
 */
public class XmlParse {

 
 //解析xml文件
 
 public static void XmlParse() throws JDOMException, IOException {
  SAXBuilder builder = new SAXBuilder();
  InputStream file = new FileInputStream("src/xml/po.xml");
  Document document = builder.build(file);//获得文档对象
  Element root = document.getRootElement();//获得根节点
  List<Element> list = root.getChildren();
  for(Element e:list) {
   System.out.println("ID="+e.getAttributeValue("id"));
   System.out.println("username="+e.getChildText("username"));
   System.out.println("password="+e.getChildText("password"));
  }
 }
 
 //增
 public static void addXml() throws JDOMException, FileNotFoundException, IOException {
  SAXBuilder builder = new SAXBuilder();
  Document doc = builder.build("src/xml/po.xml");//获得文档对象
  Element root = doc.getRootElement();//获得根节点
  
  //添加新元素
  Element element = new Element("person");
  element.setAttribute("id", "3");
  Element element1 = new Element("username");
  element1.setText("zhangdaihao");
  Element element2 = new Element("password");
  element2.setText("mima");
  element.addContent(element1);
  element.addContent(element2);
  root.addContent(element);
  doc.setRootElement(root);
  
  //文件处理
  XMLOutputter out = new XMLOutputter();
  out.output(doc, new FileOutputStream("src/xml/po.xml"));
 }
 
 //根据ID值删除一个节点
 public static void deletePerson(int id) throws JDOMException, IOException {
  SAXBuilder builder = new SAXBuilder();
  InputStream file = new FileInputStream("src/xml/po.xml");
  Document doc = builder.build(file);//获得文档对象
  Element root = doc.getRootElement();//获得根节点
  List<Element> list = root.getChildren();
  for(Element e:list) {
   //获取ID值
   if(Integer.parseInt(e.getAttributeValue("id"))==id) {
    root.removeContent(e);
    break;//??
   }
  }
  
  //文件处理
  XMLOutputter out = new XMLOutputter();
  out.output(doc, new FileOutputStream("src/xml/po.xml"));
 }
 
 //根据ID值修改一个节点
 public static void updatePerson(int id) throws JDOMException, IOException {
  SAXBuilder builder = new SAXBuilder();
  InputStream file = new FileInputStream("src/xml/po.xml");
  Document doc = builder.build(file);//获得文档对象
  Element root = doc.getRootElement();//获得根节点
  List<Element> list = root.getChildren();
  for(Element e:list) {
   //获取ID值
   if(Integer.parseInt(e.getAttributeValue("id"))==id) {
    System.out.println("--------------------");
    e.getChild("username").setText("111111111");
    e.getChild("password").setText("password");
    
   }
  }
  
  //文件处理
  XMLOutputter out = new XMLOutputter();
  out.output(doc, new FileOutputStream("src/xml/po.xml"));
 }
 
 static public void main(String ars[]) throws JDOMException, IOException {
  
 // addXml();//增加XML
 // deletePerson(3);//删除XML
 // updatePerson(2);//修改XML
 // XmlParse();//解析XML
 }
}

 

------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <person id="1">
  <username>张三</username>
  <password>123123</password>
 </person>
 <person id="2">
  <username>1111111112</username>
  <password>password2</password>
 </person>

 
</root>

   发表时间:2008-08-28  
贴完代码就完事?怎么感觉怪怪的
0 请登录后投票
   发表时间:2008-08-28  
用groovy 更简单
0 请登录后投票
   发表时间:2008-08-28  
我就不知道了

这些代码是做什么的!不就XML操作吗!

Document处理的就很多Event处理的也有几个,
你的是Document方式来处理的,如果你写一个,jdom是如何生成document过程的,我想肯定比你copy出
jdom的使用的demo更加有意义
0 请登录后投票
   发表时间:2008-12-05  
写的还行,也挺不错的!顶
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics