博客
关于我
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/

你可能感兴趣的文章
Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad++最详情汇总
查看>>
notepad++正则表达式替换字符串详解
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notes on Paul Irish's "Things I learned from the jQuery source" casts
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
NotImplementedError: Could not run torchvision::nms
查看>>
nova基于ubs机制扩展scheduler-filter
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
np.power的使用
查看>>
NPM 2FA双重认证的设置方法
查看>>
npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
查看>>
npm build报错Cannot find module ‘webpack‘解决方法
查看>>
npm ERR! ERESOLVE could not resolve报错
查看>>
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>