博客
关于我
Dom4j解析XML
阅读量:378 次
发布时间:2019-03-04

本文共 2908 字,大约阅读时间需要 9 分钟。

dom4j是一个Java/XML应用程序编程接口(API),旨在高效读取和操作XML文件。它是jdom的升级版本,性能优越、功能强大且易于使用。dom4j不仅性能优于Sun公司官方的DOM技术,还支持开源开发,可从SourceForge获取。

Jar包依赖

dom4j-2.0.0-RC1.jar

jaxen-1.1-beta-6.jar(用于XPath查询)

代码示例

以下是基于dom4j的示例代码,展示了创建、修改、删除和解析XML文档的功能。

import java.io.File;import java.io.FileWriter;import java.util.List;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;public class DOM4JDemo {    @Test    public void createXML() throws Exception {        Document document = DocumentHelper.createDocument();        Element root = document.addElement("hibernate-mapping");        Element classElement = root.addElement("class")                .addAttribute("name", "com.trs.User")                .addAttribute("table", "wcmuser");        classElement.addElement("property")                .addAttribute("name", "username")                .addText("admin");        classElement.addElement("property")                .addAttribute("name", "password")                .addText("123456");                OutputFormat format = OutputFormat.createPrettyPrint();        XMLWriter writer = new XMLWriter(new FileWriter("User.hbm.xml"), format);        writer.write(document);        writer.close();    }    @Test    public void updateXML() throws Exception {        SAXReader reader = new SAXReader();        Document document = reader.read(new File("User.hbm.xml"));        Node node = document.selectSingleNode("//hibernate-mapping/class/property[@name='password']");        node.setText("12345678");                OutputFormat format = OutputFormat.createPrettyPrint();        XMLWriter writer = new XMLWriter(new FileWriter("User.hbm.xml"), format);        writer.write(document);        writer.close();    }    @Test    public void deleteXml() throws Exception {        SAXReader reader = new SAXReader();        Document document = reader.read(new File("User.hbm.xml"));        Node node = document.selectSingleNode("//property[@name='password']");        node.getParent().remove(node);                OutputFormat format = OutputFormat.createPrettyPrint();        XMLWriter writer = new XMLWriter(new FileWriter("User.hbm.xml"), format);        writer.write(document);        writer.close();    }    @Test    public void parseXml() throws Exception {        SAXReader reader = new SAXReader();        Document document = reader.read(new File("User.hbm.xml"));        List
nodes = document.selectNodes("//hibernate-mapping/class/property"); for (Node node : nodes) { System.out.println(node.getName()); System.out.println(node.valueOf("@name")); System.out.println(node.getText()); } }}

生成的XML示例

admin
123456

dom4j是一个功能强大且易于使用的XML处理框架,适合在Java应用中进行XML操作。

转载地址:http://sxne.baihongyu.com/

你可能感兴趣的文章
Nmap扫描教程之Nmap基础知识
查看>>
nmap指纹识别要点以及又快又准之方法
查看>>
Nmap渗透测试指南之指纹识别与探测、伺机而动
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>