采用JDom实现Xml文档增加,删除,查询,修改。
- <?xml version="1.0" encoding="utf-8"?>
- <people>
- <!--将数据从程序输出到XML中!-->
- <person id="002" gender="male">
- <name>佳佳</name>
- <address>安徽</address>
- </person>
- <person id="003" gender="male">
- <name>王力宏</name>
- <address>中国</address>
- </person>
- </people>
采用如下代码进行操作:
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Iterator;
- import java.util.List;
- import org.jdom.Attribute;
- import org.jdom.Comment;
- 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;
- public class JdomTest {
- public static void main(String[] args) { // 创建文档
- try {
- // 初始化
- JdomTest.init();
- // 添加一个节点
- JdomTest.add();
- // 遍历xml
- JdomTest.traverse();
- // 删除id为1的节点
- JdomTest.del(001);
- // 编辑id为2的节点
- JdomTest.edit(002);
- } catch (JDOMException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void edit(int i) throws JDOMException, IOException {
- SAXBuilder builder = new SAXBuilder();
- String path = "jdom.xml";
- Document document = builder.build(new File(path));
- Element root = document.getRootElement();
- @SuppressWarnings("unchecked")
- List<Element> list = root.getChildren();
- Iterator<Element> it = list.iterator();
- while (it.hasNext()) {
- Element e = it.next();
- if (Integer.parseInt(e.getAttributeValue("id")) == i) {
- e.getChild("name").setText(“佳佳");
- e.getChild("address").setText("安徽");
- }
- }
- XMLOutputter out = new XMLOutputter();
- out.setFormat(out.getFormat().setEncoding("utf-8").setIndent(" "));
- out.output(document, new FileOutputStream(path));
- }
- private static void del(int i) throws JDOMException, IOException {
- SAXBuilder builder = new SAXBuilder();
- String path = "jdom.xml";
- Document document = builder.build(new File(path));
- Element root = document.getRootElement();
- @SuppressWarnings("unchecked")
- List<Element> list = root.getChildren();
- Iterator<Element> it = list.iterator();
- while (it.hasNext()) {
- Element e = it.next();
- if (Integer.parseInt(e.getAttributeValue("id")) == i) {
- root.removeContent(e);
- break;
- }
- }
- XMLOutputter out = new XMLOutputter();
- out.setFormat(out.getFormat().setEncoding("utf-8").setIndent(" "));
- out.output(document, new FileOutputStream(path));
- }
- private static void traverse() throws JDOMException, IOException {
- SAXBuilder builder = new SAXBuilder();
- String path = "jdom.xml";
- Document document = builder.build(new File(path));
- Element root = document.getRootElement();
- @SuppressWarnings("unchecked")
- List<Element> list = root.getChildren();
- Iterator<Element> it = list.iterator();
- while (it.hasNext()) {
- Element e = it.next();
- System.out.println("编号:" + e.getAttributeValue("id") + "性别:"
- + e.getAttributeValue("gender") + "姓名:"
- + e.getChild("name").getText() + "地址:"
- + e.getChild("address").getText());
- }
- }
- private static void add() throws JDOMException, IOException {
- SAXBuilder builder = new SAXBuilder();
- String path = "jdom.xml";
- Document document = builder.build(new File(path));
- Element root = document.getRootElement();
- Element person = new Element("person");
- person.setAttribute("id", "003").setAttribute("gender", "male");
- root.addContent(person);
- Element name = new Element("name");
- name.setText("王力宏");
- person.addContent(name);
- Element address = new Element("address");
- address.setText("中国");
- person.addContent(address);
- XMLOutputter out = new XMLOutputter();
- out.setFormat(out.getFormat().setEncoding("utf-8").setIndent(" "));
- out.output(document, new FileOutputStream(new File(path)));
- }
- private static void init() throws FileNotFoundException, IOException {
- // 创建文档
- Document document = new Document();
- // 创建根元素
- Element people = new Element("people");
- // 把根元素加入到document中
- document.addContent(people);
- // 创建注释
- Comment rootComment = new Comment("将数据从程序输出到XML中!");
- people.addContent(rootComment);
- // 创建父元素
- Element person1 = new Element("person");
- // 把元素加入到根元素中
- people.addContent(person1);
- // 设置person1元素属性
- person1.setAttribute("id", "001");
- Attribute person1_gender = new Attribute("gender", "male");
- person1.setAttribute(person1_gender);
- // 设置person1的姓名 并将其添加到 person1
- Element person1_name = new Element("name");
- person1_name.setText("刘德华");
- person1.addContent(person1_name);
- // 设置person1的地址 并将其添加到person1
- Element person1_address = new Element("address");
- person1_address.setText("香港");
- person1.addContent(person1_address);
- // 创建元素person2
- Element person2 = new Element("person");
- people.addContent(person2);
- // 添加属性,可以一次添加多个属性
- person2.setAttribute("id", "002").setAttribute("gender", "male");
- // 设置person2的姓名 并将其添加到 person2
- Element person2_name = new Element("name");
- person2_name.setText("林志颖");
- person2.addContent(person2_name);
- // 设置person2的地址 并将其添加到person2
- Element person2_address = new Element("address");
- person2_address.setText("台湾");
- person2.addContent(person2_address);
- // 设置xml输出格式
- Format format = Format.getPrettyFormat();
- format.setEncoding("utf-8");// 设置编码
- format.setIndent(" ");// 设置缩进
- // 得到xml输出流
- XMLOutputter out = new XMLOutputter(format);
- // 把数据输出到xml中
- out.output(document, new FileOutputStream("jdom.xml"));
- // 或者FileWriter
- }
- }
结果:
转载于:https://blog.51cto.com/splend/984811