Dom4j工具--XML的DOM解析(下)--写操作

前言:

上一篇博客我开始了用Dom4j对XML进行了读操作,这篇博客主要是进行对XML的写操作  
只涉及基础的内容,只要有javase基础和eclipse的使用 就可以完成,  
往后的内容包括  “框架”  我都还没学到,所以本文中的内容也都是局限于基础部分

有兴趣的可以参考Dom4j工具–XML的DOM解析(上)–读操作
也可以参考DOM4J官网
还可以查看DOM4J API

目录:

1. 如何写内容到XML
2. 增:文档,标签,属性,文本内容
3. 改:属性值,文本
4. 删:标签,属性

现在开始正文。

如何写内容到XML:

这一步是所有操作的前提,也是入门的必要操作

简单举个栗子:
实现对一个xml文档的粘贴复制功能

    @Test
    public void test2() throws Exception {
        // 1.读取xml文档,返回Document对象
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new File(".\\src\\day33\\ss.xml"));
        FileOutputStream fileOutputStream=new FileOutputStream("D:\\xx.xml");

        OutputFormat outputFormat1=OutputFormat.createCompactFormat();
        OutputFormat outputFormat2=OutputFormat.createPrettyPrint();
        outputFormat2.setEncoding("UTF-8");

        XMLWriter xmlWriter=new XMLWriter(fileOutputStream,outputFormat2);
        xmlWriter.write(doc);
        xmlWriter.close();
    }

结果:
createCompactFormat():

<?xml version="1.0" encoding="UTF-8"?>
<constant><cons><name>hello1</name></cons><name>hello2</name><cons><name>hello3</name></cons></constant>

createPrettyPrint():

<?xml version="1.0" encoding="UTF-8"?>

<constant> 
  <cons> 
    <name>hello1</name> 
  </cons>  
  <name>hello2</name>  
  <cons> 
    <name>hello3</name> 
  </cons> 
</constant>

注意:
1. @Test可以忽略 因为涉及到注解和单元测试,想了解单元测试的可以参考一下这篇:
单元测试–JUnit4了解一下(eclipse环境)
2. XMLWriter导入的时候注意头文件别错了 是:import org.dom4j.io.XMLWriter;
3. 这里使用FileOutputStream而不是Writer字符流 因为防止考虑编码
4. 记得最后要关闭流。
5. createCompactFormat():紧凑的结构 去除空格和换行,项目上线 因为xml更小
6. createPrettyPrint(): 漂亮的结构 有空格和换行,开发调试
outputFormat2.setEncoding(“UTF-8”);是指定保存的编码格式为UTF-8
修改:
outputFormat2.setEncoding(“GBK”);内容是中文的话 就会出现乱码
outputFormat2.setEncoding会使得 保存的编码格式和文档声明一致 即

<?xml version="1.0" encoding="GBK"?>

增:文档,标签,属性,文本内容:

DocumentHelper.createDocument() 增加文档
addElement("名称") 增加标签
addAttribute("名称",“值”) 增加属性
addText(“内容”) 增加文本内容

源代码:

@Test
    public void test2() throws Exception {

        FileOutputStream fileOutputStream=new FileOutputStream("D:\\xx.xml");

        Document doc=DocumentHelper.createDocument();//创建文档
        Element element=doc.addElement("age1");      //创建标签
        element.addAttribute("id", "12");            //创建属性
        element.addText("Text");                     //创建文本文件

        OutputFormat outputFormat2=OutputFormat.createPrettyPrint();
        outputFormat2.setEncoding("UTF-8");
        XMLWriter xmlWriter=new XMLWriter(fileOutputStream,outputFormat2);
        xmlWriter.write(doc);
        xmlWriter.close();
    }

注意:
Element element=doc.addElement(“age1”);不能重复插入,因为只有一个根标签

修改:属性值,文本:

Attribute.setValue("值")  修改属性值
Element.addAtribute("同名的属性名","值")  修改同名的属性值
Element.setText("内容")  修改文本内容
    @Test
    public void test2() throws Exception {
        // 1.读取xml文档,返回Document对象
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new File(".\\src\\day33\\ss.xml"));
        FileOutputStream fileOutputStream=new FileOutputStream("D:\\xx.xml");

        //获取标签对象
        Element element=doc.getRootElement();
        Element elementSon=element.element("cons");

        //修改属性值
        elementSon.addAttribute("id", "12");            // 通过增加同名属性的方法,修改属性值
        Attribute attribute=elementSon.attribute("id"); // 获取属性对象 修改属性值值
        attribute.setValue("13");;

        elementSon.setText("修改1");                     //修改文本内容


        OutputFormat outputFormat2=OutputFormat.createPrettyPrint();
        outputFormat2.setEncoding("UTF-8");
        XMLWriter xmlWriter=new XMLWriter(fileOutputStream,outputFormat2);
        xmlWriter.write(doc);
        xmlWriter.close();
    }

注意:cons标签下先文本后name标签,如果修改cons文本内容后 ,文本会下移到name标签下。似乎没啥大影响

删:标签,属性:

Element.detach();  删除标签  
Attribute.detach();  删除属性

源代码:

@Test
    public void test2() throws Exception {
        // 1.读取xml文档,返回Document对象
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new File(".\\src\\day33\\ss.xml"));
        FileOutputStream fileOutputStream=new FileOutputStream("D:\\xx.xml");

        //获取标签对象
        Element element=doc.getRootElement();
        Element elementSon=element.element("cons");

        //获取到cons下第一个标签节点并且删除
        elementSon.elements().get(0).detach();

        //获取到cons标签的属性为id的属性对象,然后删除
        Attribute idAttribute=elementSon.attribute("id");
        idAttribute.detach();


        OutputFormat outputFormat2=OutputFormat.createPrettyPrint();
        outputFormat2.setEncoding("UTF-8");
        XMLWriter xmlWriter=new XMLWriter(fileOutputStream,outputFormat2);
        xmlWriter.write(doc);
        xmlWriter.close();
    }

猜你喜欢

转载自blog.csdn.net/qq_38409944/article/details/80549122