Java 使用jdom 创建、解析、追加、删除Xml

jar包

<dependency>
    <groupId>org.jdom</groupId>
    <artifactId>jdom2</artifactId>
    <version>2.0.6</version>
</dependency>

1.创建Xml

/**
     * 创建XMl文件
     */
    public static void createXml(){
        try{
            //1.生成一个根节点
            Element root = new Element("xml");
            //2.创建一个document对象
            Document document = new Document(root);

            //3.创建根里的节点内容
            for (int i = 1;i <= 5;i++){
                //创建一个person节点
                Element person = new Element("person");
                //person节点添加节点属性
                person.setAttribute("id",String.valueOf(i));

                Element id = new Element("id");//创建id节点
                //这里说明一下,new CDATA()生成xml后节点是:<id><![CDATA[value]]></id>
                id.addContent(new CDATA(String.valueOf(i)));
                //如果使用的是setText(),生成xml则是正常的value <id>value</id>
//                id.setText("1");
                person.addContent(id);

                Element name = new Element("name");//创建name节点
//                name.setText("小明");//学生姓名
                name.addContent(new CDATA("小明"));
                person.addContent(name);

                Element card = new Element("card");//创建card节点
//                card.setText("1001");//card存为学号
                card.addContent(new CDATA("1001"));
                person.addContent(card);


                root.addContent(person);
            }

            Format format = Format.getCompactFormat();
            format.setIndent("  ");//设置换行Tab或空格
            format.setEncoding("UTF-8");//设置字符

            //4.创建Xml输出对象XMLOutputter
            XMLOutputter outputter = new XMLOutputter(format);

            File file = new File("person.xml");
            //5.用outputter 将document 转成xml文件
            outputter.output(document,new FileOutputStream(file));

            System.out.println("生成XML文件成功!");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

说明:
这里说明一下,new CDATA()生成xml后节点是:<![CDATA[value]]>
id.addContent(new CDATA(String.valueOf(i)));

如果使用的是setText(),生成xml则是正常的value
id.setText(“1”);

在这里插入图片描述

2.解析Xml

/**
     * 解析Xml文件
     */
    public static void analyzeXml(){
        try{
            File file = new File("person.xml");

            //创建一个解析器
            SAXBuilder builder = new SAXBuilder();

            //创建一个输入流
            InputStream in = new FileInputStream(file);
            //将流加载到解析器中,获取document对象
            Document document = builder.build(in);
            //获取xml根节点
            Element root = document.getRootElement();
            //getChild 是获取root节点下第一个person节点
//            Element element = root.getChild("person");
            //getChildren 是获取root节点下所有person节点
            List<Element> personList =  root.getChildren("person");
            //循环所有的person节点
            for (Element element : personList) {
                System.out.println("element = " + element.getAttributeValue("id"));
                //是获取person节点下所有节点
                List<Element> els = element.getChildren();
                for (Element el : els) {
                    System.out.println(el.getName() + " : " + el.getText());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

说明:
getChild 是获取root节点下第一个person节点
Element element = root.getChild(“person”);

getChildren 是获取root节点下所有person节点
List personList = root.getChildren(“person”);

在这里插入图片描述

3.追加Xml节点

/**
     * 追加节点
     */
    public static void addToXml(){
        try{
            File file = new File("person.xml");

            //创建一个解析器
            SAXBuilder builder = new SAXBuilder();
            //创建输入流
            InputStream in = new FileInputStream(file);
            //将流加载到解析器中,获取document对象
            Document document = builder.build(in);

            //获取根节点
            Element root = document.getRootElement();

            //创建一个person节点
            Element person = new Element("person");
            //person节点添加节点属性
            person.setAttribute("id",String.valueOf("6"));

            Element id = new Element("id");//创建id节点
            //这里说明一下,new CDATA()生成xml后节点是:<id><![CDATA[value]]></id>
            id.addContent(new CDATA(String.valueOf("6")));
            //如果使用的是setText(),生成xml则是正常的value <id>value</id>
//                id.setText("1");
            person.addContent(id);

            Element name = new Element("name");//创建name节点
//                name.setText("小明");//学生姓名
            name.addContent(new CDATA("JACK"));
            person.addContent(name);

            Element card = new Element("card");//创建card节点
//                card.setText("1001");//card存为学号
            card.addContent(new CDATA("1006"));
            person.addContent(card);

            root.addContent(person);

            Format format = Format.getCompactFormat();
            format.setIndent("  ");//设置换行Tab或空格
            format.setEncoding("UTF-8");//设置字符
            //创建Xml输出对象XMLOutputter
            XMLOutputter outputter = new XMLOutputter(format);

            //用outputter 将document 转成xml文件
            outputter.output(document,new FileOutputStream("person.xml"));

            System.out.println("追加节点成功!");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

在这里插入图片描述
4.删除Xml节点

/**
     * 删除XMl节点
     */
    public static void deleteXml(){
        try{
            File file = new File("person.xml");

            SAXBuilder builder = new SAXBuilder();

            InputStream in = new FileInputStream(file);

            Document document = builder.build(in);
            //获取根节点
            Element root = document.getRootElement();

            List<Element> personList = root.getChildren("person");
            for (Element element : personList) {
                String id = element.getAttributeValue("id");
                if(id.equals("6")){
                    root.removeContent(element);
                    break;
                }
            }

            Format format = Format.getCompactFormat();
            format.setIndent("  ");//设置换行Tab
            format.setEncoding("UTF-8");

            XMLOutputter outputter = new XMLOutputter(format);
            //利用outputer将document转换成xml文档
            outputter.output(document,new FileOutputStream(file));

            System.out.println("删除节点成功!");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

测试:

    public static void main(String[] arg0){
//        createXml();
//        analyzeXml();
//        addToXml();
//        deleteXml();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值