java jdom进行xml的增删改差_用Jdom对xml进行增删改

public class XmlTest {

//查询所有的数据

public static void list() throws JDOMException, IOException {

SAXBuilder builder = new SAXBuilder();

InputStream file = new FileInputStream("src/po.xml");

//System.out.println(file.available());

//获得文档对象

Document document = builder.build(file);

//获得根节点

Element root = document.getRootElement();

List list = root.getChildren();

System.out.println("root : "+root);

System.out.println(root.getName());

Iterator it = list.iterator();

//for(Element e:list) {

//  for(int i = 0; i < list.size(); i++){

while(it.hasNext()){

Element e = (Element) it.next();

System.out.println("ID: "+e.getAttributeValue("id"));

System.out.println(e.getChildText("username"));

System.out.println(e.getChildText("password"));

//System.out.println(list.iterator().getClass().toString());

}

}

//添加数据

public static void add() throws JDOMException, FileNotFoundException, IOException {

SAXBuilder builder = new SAXBuilder();

Document document = builder.build("src/po.xml");

Element root = document.getRootElement();

Element element = new Element("person");

element.addAttribute("id","3");

Element e1 = new Element("username");

e1.setText("hello");

Element e2 = new Element("password");

e2.setText("world");

element.addContent(e1);

element.addContent(e2);

root.addContent(element);

document.setRootElement(root);

XMLOutputter output = new XMLOutputter();

output.output(document,new FileOutputStream("src/po.xml"));

}

//修改数据

public static void edit(int id) throws JDOMException, FileNotFoundException, IOException {

SAXBuilder builder = new SAXBuilder();

Document document = builder.build("src/po.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("wuchao");     e.getChild("password").setText("jiayou");         }   }   XMLOutputter output = new XMLOutputter();   output.output(document,new FileOutputStream("src/po.xml"));  }    //删除  public static void del(int id) throws JDOMException, FileNotFoundException, IOException {   SAXBuilder builder = new SAXBuilder();   Document document = builder.build("src/po.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/po.xml"));  }  public static void main(String[] args) {    //   XmlTest.add(); //   XmlTest.edit(1);    try {     XmlTest.list();    } catch (JDOMException e1) {     // TODO 自动生成 catch 块     e1.printStackTrace();    } catch (IOException e1) {     // TODO 自动生成 catch 块     e1.printStackTrace();    }    try {     try {      XmlTest.del(1);     } catch (FileNotFoundException e) {      // TODO 自动生成 catch 块      e.printStackTrace();     } catch (IOException e) {      // TODO 自动生成 catch 块      e.printStackTrace();     }    } catch (JDOMException e) {     // TODO 自动生成 catch 块     e.printStackTrace();    }     } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值