Jdom解析、生成XML文件

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文件的解释。

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vgbh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值