java使用dom4j操作xml java使用dom4j操作xml代码实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java使用dom4j操作xml java使用dom4j操作xml代码实例

  2021-03-19 我要评论
想了解java使用dom4j操作xml代码实例的相关内容吗,在本文为您仔细讲解java使用dom4j操作xml的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,dom4j,操作xml,下面大家一起来学习吧。

dom4j是一个非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源工具。可以在这个地址http://dom4j.sourceforge.net进行下载。
这里我们使用到的dom4j是dom4j-1.6.1这个版本,我们只需要使用到如下两个jar包:

复制代码 代码如下:

dom4j-1.6.1.jar
commons-io-2.4.jar

1、dom4j读取xml字符串

复制代码 代码如下:

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


public class TestReadXMLString {
    public static void main(String[] args) throws DocumentException {
        String readline = "<?xml version=\"1.0\" encoding=\"utf-8\"?><students><student sid=\"001\"> <id>001</id><name>灰机</name> <age>18</age> </student></students>";
        Document document = DocumentHelper.parseText(readline);
        Element rootElm = document.getRootElement();
        System.out.println("rootElement:  " + rootElm.getName());
        Element student = rootElm.element("student");
        Element id = student.element("id");
        Element name = student.element("name");
        Element age = student.element("age");
        System.out.println(id.getText());
        System.out.println(name.getText());
        System.out.println(age.getText());
    }
}

2、dom4j创建xml文件

复制代码 代码如下:

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
public class TestWriteXMLString {
    public static void main(String[] args) {
        OutputFormat format = OutputFormat.createPrettyPrint();
        // 1. 构造空的Document
        Document doc = DocumentHelper.createDocument();
        doc.addComment("this is a comment");
        // 2. 构造根元素
        Element rootElmt = doc.addElement("users");
        rootElmt.addNamespace("test", "www.test.com");

        Element userElmt = rootElmt.addElement("user");
        userElmt.addAttribute("number", "1001");
        userElmt.addElement("name").setText("zhangsan");
        userElmt.addElement("age").setText("20");
        userElmt.addElement("gender").setText("mail");

        Element userElmt2 = rootElmt.addElement("user");
        userElmt.addAttribute("number", "1002");
        userElmt2.addElement("name").setText("zhangsan");
        userElmt2.addElement("age").setText("20");
        userElmt2.addElement("gender").setText("mail");

        System.out.println(doc.asXML().replaceAll("\n", ""));
    }
}

3、读取或写xml文件

读取xml文件

复制代码 代码如下:

SAXReader reader = new SAXReader();
String path = "E:/Workspaces/MyEclipse 8.6/xmltest/file/student.xml";
Document document = reader.read(new File(path));

写xml文件

复制代码 代码如下:

OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");// 设置XML文件的编码格式
String filePath = "E:/Workspaces/MyEclipse 8.6/xmltest/file/student.xml";
Document document = DocumentHelper.createDocument();
doc.addComment("this is a comment");

/创建document内容

复制代码 代码如下:

XMLWriter writer = new XMLWriter(new FileWriter(filePath), format);//写入指定的文件
writer.write(document);
 writer.close();

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们