jdom操作xml文件

package com.jdom;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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.Format;
import org.jdom.output.XMLOutputter;

@SuppressWarnings("unchecked")
public class XmlUtil {

 /**
  * 解析xml
  *
  * @throws IOException
  * @throws JDOMException
  */
 public static void xmlParse(File file) throws JDOMException, IOException {
  SAXBuilder builder = new SAXBuilder();
  InputStream in = new FileInputStream(file);
  // 获得文档对象
  Document doc = builder.build(in);
  // 获得跟节点
  Element root = doc.getRootElement();
  List<Element> list = root.getChildren();
  for (Element e : list) {
   System.out.println("id:" + e.getAttributeValue("id") + "  "
     + "username:" + e.getChildText("username") + "  "
     + "password:" + e.getChildText("password"));
  }
 }

 /**
  * 增加元素
  *
  * @param file
  * @throws JDOMException
  * @throws IOException
  */
 public static void addXml(File file) throws JDOMException, IOException {
  SAXBuilder builder = new SAXBuilder();
  Document doc = builder.build(file);
  Element root = doc.getRootElement();
  // 创建新元素
  Element e = new Element("user");
  e.setAttribute("id", "3");
  Element e1 = new Element("username");
  e1.setText("王亮亮");
  Element e2 = new Element("password");
  e2.setText("2006");
  e.addContent(e1);
  e.addContent(e2);
  root.addContent(e);
  doc.setRootElement(root);

  // 文件处理
  XMLOutputter out = new XMLOutputter();
  // 设置格式和编码
  // Format format = Format.getPrettyFormat();
  // format.setEncoding("UTF-8");
  out.setFormat(Format.getPrettyFormat().setEncoding("UTF-8"));
  out.output(doc, new FileOutputStream(file));
 }

 /**
  * 根据id修改元素
  *
  * @param id
  * @param username
  * @param password
  * @param file
  * @throws JDOMException
  * @throws IOException
  */
 public static void updateXml(int id, String username, String password,
   File file) throws JDOMException, IOException {
  SAXBuilder builder = new SAXBuilder();
  InputStream in = new FileInputStream(file);
  Document doc = builder.build(in);
  Element root = doc.getRootElement();
  List<Element> list = root.getChildren();
  for (Element e : list) {
   if (id == Integer.parseInt(e.getAttributeValue("id"))) {
    e.getChild("username").setText(username);
    e.getChild("password").setText(password);
   }
  }

  // 文件处理
  XMLOutputter out = new XMLOutputter();
  out.output(doc, new FileOutputStream(file));
 }

 /**
  * 根据id删除元素
  *
  * @param id
  * @param file
  * @throws JDOMException
  * @throws IOException
  */
 public static void deleteXml(int id, File file) throws JDOMException,
   IOException {
  SAXBuilder builder = new SAXBuilder();
  InputStream in = new FileInputStream(file);
  Document doc = builder.build(in);
  Element root = doc.getRootElement();
  List<Element> list = root.getChildren();
  for (Element e : list) {
   if (id == Integer.parseInt(e.getAttributeValue("id"))) {
    root.removeContent(e);
    break;
   }
  }

  // 文件处理
  XMLOutputter out = new XMLOutputter();
  out.output(doc, new FileOutputStream(file));
 }

 public static void main(String[] args) {
  try {
   File file = new File("src/com/util/user.xml");
   // XmlUtil.addXml(file);
   // XmlUtil.updateXml(3, "王会赢", "2010", file);
   // XmlUtil.deleteXml(3, file);
   XmlUtil.xmlParse(file);
  } catch (JDOMException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}
---------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<users>
  <user id="1">
    <username>刘意</username>
    <password>2008</password>
  </user>
  <user id="2">
    <username>陈立飞</username>
    <password>520</password>
  </user>
</users>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值