VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • Java操作XML(6)--使用dom4j处理XML

   dom4j是一个用于处理XML的开源框架,该框架集成了XPath,完全支持DOM,SAX,JAXP。本文主要介绍使用dom4j来处理XML,文中所使用到的软件版本:Java 1.8.0_191、dom4j 2.1.3、jaxen 1.2.0。

1、前言

    dom4j是一个十分优秀的JavaXML API,具有性能优异、功能强大和极其易使用的特点。DOM4J性能很好,连Sun的JAXM也在用DOM4J。目前许多开源项目中大量采用DOM4J,例如Hibernate使用DOM4J来读取XML配置文件。

2、使用dom4j操作XML

2.1、XML文件

复制代码
<?xml version="1.0" encoding="utf-8" ?>
<school:grade xmlns:school="http://www.w3.org/TR/html4/school/">
    <school:student rollno="1" school:age="10">
        <school:firstname>cxx1</school:firstname>
        <lastname>Bob1</lastname>
        <nickname>stars1</nickname>
        <marks>85</marks>
    </school:student>
    <student rollno="2">
        <firstname>cxx2</firstname>
        <lastname>Bob2</lastname>
        <nickname>stars2</nickname>
        <marks>85</marks>
    </student>
    <student rollno="3">
        <firstname>cxx3</firstname>
        <lastname>Bob3</lastname>
        <nickname>stars3</nickname>
        <marks>85</marks>
    </student>
</school:grade>
复制代码

2.2、引入依赖

<dependency>
    <groupId>org.dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>2.1.3</version>
</dependency>

2.3、Java代码例子

该例子演示了使用dom4j解析XML、dom4j中XPath的使用以及使用dom4j来生成XML。

复制代码
package com.abc.demo.general.xml;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.util.List;

/**
 * 使用dom4j处理xml
 */
public class Dom4jCase {
    private static Logger logger = LoggerFactory.getLogger(Dom4jCase.class);

    /**
     * 解析xml
     * @throws Exception
     */
    @Test
    public void parse() throws Exception {
        SAXReader reader = new SAXReader();
        Document document = reader.read(DomCase.class.getResourceAsStream("student.xml"));
        //Document document = DocumentHelper.parseText(xmlString);//解析字符串
        Element root = document.getRootElement();
        List<Element> list = root.elements("student");
        for (int i = 0; i < list.size(); i++) {
            Element student = list.get(i);
            logger.info("学生编号{}", student.attributeValue("rollno"));
            logger.info("age:{}", student.attributeValue("age"));
            logger.info("firstname:{}", student.elementText("firstname"));
            logger.info("lastname:{}", student.elementText("lastname"));
            logger.info("nickname:{}", student.elementText("nickname"));
            logger.info("marks:{}", student.elementText("marks"));
        }
    }

    /**
     * xpath使用
     * @throws Exception
     */
    @Test
    public void xpath() throws Exception {
        SAXReader reader = new SAXReader();
        Document document = reader.read(DomCase.class.getResourceAsStream("student.xml"));
        logger.info("查找所有的存在rollno属性的student节点,取出lastname的值");
        List<Node> list = document.selectNodes("//student[@rollno]/lastname/text()");//第二、三位学生
//        List<Node> list = document.selectNodes("//student[@rollno]/child::lastname/test()");//效果同上
        for (int i = 0; i < list.size(); i++) {
            Node node = list.get(i);
            logger.info(node.getText());
        }

        logger.info("查找所有的存在rollno属性的student节点");
        list = document.selectNodes("//school:student[@rollno]");//第一位学生
        for (int i = 0; i < list.size(); i++) {
            Node node = list.get(i);
            //获取rollno的属性值
            logger.info(node.valueOf("@rollno"));
        }

    }

    /**
     * 生成xml
     */
    @Test
    public void toXml() throws Exception {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("school:class", "http://www.w3.org/TR/html4/school/");
        //第一个学生
        Element student = root.addElement("school:student");
        student.addAttribute("rollno", "1");
        student.addAttribute("school:age", "10");
        student.addElement("school:firstname").setText("cxx1");
        student.addElement("lastname").setText("Bob1");
        student.addElement("nickname").setText("stars1");
        student.addElement("marks").setText("85");
        //第二个学生
        student = root.addElement("student");
        student.addAttribute("rollno", "2");
        student.addElement("school:firstname").setText("cxx2");
        student.addElement("lastname").setText("Bob2");
        student.addElement("nickname").setText("stars2");
        student.addElement("marks").setText("85");
        //第三个学生
        student = root.addElement("student");
        student.addAttribute("rollno", "3");
        student.addElement("school:firstname").setText("cxx3");
        student.addElement("lastname").setText("Bob3");
        student.addElement("nickname").setText("stars3");
        student.addElement("marks").setText("85");

        //直接转成字符串
//        logger.info(document.asXML());

        //格式化并输出到输出流
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(document);
        logger.info(out.toString());
    }
}
复制代码


 

来源:https://www.cnblogs.com/wuyongyin/p/14273893.html

相关教程