欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Jdom解析、生成XML文件

程序员文章站 2022-03-03 15:54:30
...

JDOM只是一种适合Java程序员来使用的Java XML API。

要用Jdom进行开发,需要到jar包,因为是开源的,所以GitHub上也有,
1、http://jdom.org/downloads/source.html
2、https://github.com/hunterhacker/jdom/

第一个可以获取到jar包,第二个就是Jdom的源码了

代码如下:

package com.vgbh;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

public class JdomXMLDemo {

    public static void main(String[] args) {
        JdomXMLDemo jxd = new JdomXMLDemo();

        String uri = "bookstore.xml";
        String uri1 = "F:/WorkSpace/JdomDemoD.xml";

        //jxd.insertJdomXMLDemo(uri1);

        //jxd.queryJdomXMLDemo(uri);

        jxd.updateJdomXMLDemo(uri,"0");
    }

    //写入XML文件
    public void insertJdomXMLDemo (String uri) {
        //添加根节点
        Element bookstore = new Element("bookstore"); 
        //添加元素
        Element book = new Element("book");
        //添加元素的本地名称
        Element name = new Element("name");
        Element author = new Element("author");
        Element price = new Element("price");
        Element language = new Element("language");
        Element country = new Element("country");

        //Document对象
        Document doc = new Document(bookstore);

        //元素的数据填充
        name.setText("names");
        author.setText("authors");
        price.setText("prices");
        language.setText("languages");
        country.setText("countrys");

        //数据添加入元素
        book.addContent(name);
        book.addContent(author);
        book.addContent(price);
        book.addContent(language);
        book.addContent(country);
        //将元素添加进bookstore
        bookstore.addContent(book);

        //将doc转换为XML文件
        Format format = Format.getCompactFormat();
        format.setEncoding("UTF-8");//设置编码格式
        format.setIndent("    ");//设置首行缩进

        //定义输出对象
        XMLOutputter XMLOut = new XMLOutputter();
        try {
            //将doc转换为XML
            XMLOut.output(doc, new FileOutputStream(uri));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("写入成功!");

    }

    //读取XML文件
    public void queryJdomXMLDemo (String uri) { 
        try {
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(uri);

            Element book = doc.getRootElement();
            List list = book.getChildren("book");

            for (int i = 0; i < list.size(); i++) {
                Element e = (Element) list.get(i);
//              e.getChildText("name");
//              e.getChildText("author");
//              e.getChildText("price");
//              e.getChildText("language");
//              e.getChildText("country");

//              Element name = e.getChild("name");
//              name.setText("xiugaiguode ");

                System.out.println("name:" + e.getChildText("name") + "    author:" + e.getChildText("author") + "     price:" + 
                e.getChildText("price") + "    lanuage:" + e.getChildText("language") + "    country:" + e.getChildText("country"));

            }


        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //修改XML文件
    public void updateJdomXMLDemo (String uri, String id) {
        try {
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(uri);

            Element book = doc.getRootElement();
            List list = book.getChildren("book");

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

                if (String.valueOf(i).equals(id)) {
                    Element e = (Element) list.get(i);
//                  e.getChildText("name");
//                  e.getChildText("author");
//                  e.getChildText("price");
//                  e.getChildText("language");
//                  e.getChildText("country");

                    Element name = e.getChild("name");
                    name.setText("xiugaiguode ");

                    System.out.println("name:" + e.getChildText("name") + "    author:" + e.getChildText("author") + "     price:" + 
                    e.getChildText("price") + "    lanuage:" + e.getChildText("language") + "    country:" + e.getChildText("country"));
                }

            }

            //将doc转换为XML文件
            Format format = Format.getCompactFormat();
            format.setEncoding("UTF-8");//设置编码格式
            format.setIndent("    ");//设置首行缩进

            //定义输出对象
            XMLOutputter XMLOut = new XMLOutputter();
            //将doc转换为XML
            XMLOut.output(doc, new FileOutputStream(uri));

            System.out.println("修改成功!");

        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

不理解的可以去看看我另外的几篇关于解析、生成XML文件的解释。

有问题可以联系我的邮箱。