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>