DOM4J-具体实现

ASM磁盘通过rebalance迁移 一、实施要点: 1、数据迁移前,如果库不大,最好全库备份一次 2、进行数据平衡时,数据磁盘是添加到现有磁盘组,而不是重新建一个磁盘组 3、本次实施过程,服务器只有1张HBA卡,本次实施先取消原有的多路径,只保留旧有存储单路径,另一个FC口插上新存储的路径。待迁移完成后,再配置新存储的多路径 二、实施步骤 1.查询并配置ASM磁盘 [root@rac1 ~]# /sbin/scsi_id... 阅读详情


转自:https://dom4j.github.io/

Getting started

Home

Parsing XML

One of the first things you'll probably want to do is to parse an XML document of some kind. This is easy to do in <dom4j>. The following code demonstrates how to this.

import java.net.URL;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

public class Foo {

    public Document parse(URL url) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(url);
        return document;
    }
}

Using Iterators

A document can be navigated using a variety of methods that return standard Java Iterators. For example

public void bar(Document document) throws DocumentException {

    Element root = document.getRootElement();

    // iterate through child elements of root
    for (Iterator<Element> it = root.elementIterator(); it.hasNext();) {
        Element element = it.next();
        // do something
    }

    // iterate through child elements of root with element name "foo"
    for (Iterator<Element> it = root.elementIterator("foo"); it.hasNext();) {
        Element foo = it.next();
        // do something
    }

    // iterate through attributes of root
    for (Iterator<Attribute> it = root.attributeIterator(); it.hasNext();) {
        Attribute attribute = it.next();
        // do something
    }
 }

Powerful Navigation with XPath

In <dom4j> XPath expressions can be evaluated on the Document or on any Node in the tree (such as Attribute,Element or ProcessingInstruction). This allows complex navigation throughout the document with a single line of code. For example

public void bar(Document document) {
    List<Node> list = document.selectNodes("//foo/bar");

    Node node = document.selectSingleNode("//foo/bar/author");

    String name = node.valueOf("@name");
}

For example if you wish to find all the hypertext links in an XHTML document the following code would do the trick.

public void findLinks(Document document) throws DocumentException {

    List<Node> list = document.selectNodes("//a/@href");

    for (Iterator<Node> iter = list.iterator(); iter.hasNext();) {
        Attribute attribute = (Attribute) iter.next();
        String url = attribute.getValue();
    }
}

If you need any help learning the XPath language we highly recommend the Zvon tutorial which allows you to learn by example.

Fast Looping

If you ever have to walk a large XML document tree then for performance we recommend you use the fast looping method which avoids the cost of creating an Iterator object for each loop. For example

public void treeWalk(Document document) {
    treeWalk(document.getRootElement());
}

public void treeWalk(Element element) {
    for (int i = 0, size = element.nodeCount(); i < size; i++) {
        Node node = element.node(i);
        if (node instanceof Element) {
            treeWalk((Element) node);
        }
        else {
            // do something…
        }
    }
}

Creating a new XML document

Often in <dom4j> you will need to create a new document from scratch. Here's an example of doing that.

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

public class Foo {

    public Document createDocument() {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("root");

        Element author1 = root.addElement("author")
            .addAttribute("name", "James")
            .addAttribute("location", "UK")
            .addText("James Strachan");

        Element author2 = root.addElement("author")
            .addAttribute("name", "Bob")
            .addAttribute("location", "US")
            .addText("Bob McWhirter");

        return document;
    }
}

Writing a document to a file

A quick and easy way to write a Document (or any Node) to a Writer is via the write() method.

FileWriter out = new FileWriter("foo.xml");
document.write(out);
out.close();

If you want to be able to change the format of the output, such as pretty printing or a compact format, or you want to be able to work with Writer objects or OutputStream objects as the destination, then you can use the XMLWriter class.

import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class Foo {

    public void write(Document document) throws IOException {

        // lets write to a file
        try (FileWriter fileWiter = new FileWriter("output.xml")) {
            XMLWriter writer = new XMLWriter(fileWriter);
            writer.write( document );
            writer.close();
        }


        // Pretty print the document to System.out
        OutputFormat format = OutputFormat.createPrettyPrint();
        writer = new XMLWriter(System.out, format);
        writer.write( document );

        // Compact format to System.out
        format = OutputFormat.createCompactFormat();
        writer = new XMLWriter(System.out, format);
        writer.write(document);
        writer.close();
    }
}

Converting to and from Strings

If you have a reference to a Document or any other Node such as an Attribute or Element, you can turn it into the default XML text via the asXML() method.

Document document = …;
String text = document.asXML();

If you have some XML as a String you can parse it back into a Document again using the helper method DocumentHelper.parseText()

String text = "<person> <name>James</name> </person>";
Document document = DocumentHelper.parseText(text);

Transforming a Document with XSLT

Applying XSLT on a Document is quite straightforward using the JAXP API from Oracle. This allows you to work against any XSLT engine such as Xalan or Saxon. Here is an example of using JAXP to create a transformer and then applying it to a Document.

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;

import org.dom4j.Document;
import org.dom4j.io.DocumentResult;
import org.dom4j.io.DocumentSource;

public class Foo {

    public Document styleDocument(Document document, String stylesheet) throws Exception {

        // load the transformer using JAXP
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));

        // now lets style the given document
        DocumentSource source = new DocumentSource(document);
        DocumentResult result = new DocumentResult();
        transformer.transform(source, result);

        // return the transformed document
        Document transformedDoc = result.getDocument();
        return transformedDoc;
    }
}


Ubuntu20.04安装NVIDIA显卡驱动避坑指南:从.run文件到完美运行 本文详细介绍了在Ubuntu20.04系统上通过.run文件安装NVIDIA显卡驱动的完整流程和避坑技巧。从禁用Nouveau驱动、选择合适显示管理器到处理常见安装问题,提供了一套经过验证的解决方案,帮助用户顺利完成驱动安装并优化性能。 阅读详情

相关推荐

非标准包 game.rgss3a 的打开方法 | 2023 年实测

生活笔记

陌雨’的博客 1万+

domfj

Last published: 16 May 2005 | Doc for 1.6.1 XPath Spec | Jaxen | dom4j@SourceForge dom4j Home FAQ Quick start Cookbook Comparison Goals Changes Download License Javadoc (1.

tiantian715的专栏 801

实战指南:在华为防火墙上构建基于自签CA的SSL证书挑战式认证体系

本文详细介绍了在华为防火墙上构建基于自签CA的SSL证书挑战式认证体系的实战指南。通过自签CA证书生成、华为防火墙SSL VPN配置、客户端安装与测试等步骤,帮助企业实现更安全的远程办公接入方案,有效防范密码泄露风险。文章重点讲解了证书挑战登录的实现原理和配置细节,适用于企业网络安全建设。

weixin_32428571的博客 256

JAVA基于dom4j实现对XML操作

该篇博客写java基于dom4j来操作xml的一些基本实现,需要用到以下jar包 该篇博客目录 1、XML特点、语法规则 2、XML与HTML区别 3、基于dom4j下java实现对XML基本操作 一、XML特点、语法规则 1、XML特点 XML是一种标记语言,很类似HTML XML的设计宗旨是传输数据,而非显示数据(HTML) XML标签没...

浅然的专栏 1万+

图像传感器与信号处理——详解CCD与CMOS图像传感器

一文详解CCD与CMOS图像传感器一文详解CCD与CMOS图像传感器1. 图像传感器性能关键词1.1 噪声1.2 暗电流 一文详解CCD与CMOS图像传感器 本文主要总结了CCD与CMOS传感器的原理知识,参考书籍为《数码相机中图像传感器和信号处理》,英文版为《Image Sensors And Signal Processing for Digital Still Cameras》 1. 图像传...

weixin_44580210的博客 2万+

blender中合并的模型,在threejs中显示多个mesh;blender多材质烘培成一个材质

5、优化项(省略此步骤,直接进行步骤6),进入到视图着色方式(此时没有灯光,整体偏暗,如果不调整灯光,烘培出来的材质也偏暗,因此此步骤为优化项,通畅建议调整)。在右边栏进入世界环境,调整颜色。7、烘焙成功之后,新建一个材质,将第三步创建的纹理图像作为材质的纹理,连接到基础色,并把模型的材质改为此材质,其他的材质全部删除。2、处理uv,在侧边栏选择数据-uv贴图,选中使用的uv,进入编辑模式,全选,键盘u,选择智能uv投射。3、处理材质,在侧边栏选择材质,进入着色,在着色器编辑器新建一个图像纹理,新建图像。

AllBluefm的专栏 2529

Java dom4j生成和解析XML

dom4j 使用:需先导入dom4j对应的jar,本章用的是dom4j-1.6.1.jar 优点:DOM4J使Java开发的灵活性和XML解析代码易于维护 dom4j相关操作类 Document:表示整个xml文档,是一个树形结构 Eelment:表示一个xml的元素,提供方法操作其子元素,它的文本,属性和名称空间 Attribute:表示元素的属性 Node:表示元素,属性 do...

p812438109的专栏 5万+

从入门到精通|Yalmip+Cplex在电力系统中的应用(Matlab代码实现)

​ 火电成本包括运行成本、碳捕集成本,其中火电运行成本由运行维护成 本和发电煤耗成本构成,发电煤耗与其出力关系: ,F 为机组煤耗量(kg/h),P 为机组出力/MW;运行维护成本按照 0.5 倍煤耗成本考虑,碳捕集成本取决于碳排放量及碳捕集单价,火电机组相关参数如附表 1 所示,电煤价格为 700 元/t ​

weixin_46039719的博客 1552

Java 使用 Dom4j 解析 XML 指南

Java 使用 Dom4j 解析 XML 指南 太阳火神的美丽人生 (http://blog.youkuaiyun.com/opengl_es) 本文遵循“署名-非商业用途-保持一致”创作公用协议 转载请保留此句:太阳火神的美丽人生 - 本博客专注于敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博...

weixin_33920401的博客 133

基于libVLC的视频播放器之四:直接使用libVLC

一.效果 播放本地文件 播放网络url:rtsp://3.84.6.190/vod/mp4:BigBuckBunny_115k.mov 二.实现 既然有VLC-Qt,为什么还要直接调用libVLC呢,因为直接调用会加深对整个播放流程的理解,方便后面修改VLC-Qt源码。 注意事项: 1.编译报错:error C2065: “libvlc_media_read_cb”: 未声明的标识符。修改如下: 2.libvlc_new(0, nullptr)返回null 将plugins

草上爬的博客 5048

get_ccopt系列命令介绍

get_ccopt_property:用来获取cts阶段,clock tree上所有的参数信息,可以通过get_ccopt_property –help *获取所有参数。get_ccopt_skew_group_delay:用来获取skew group的相关信息,可以通过筛选出特定条件下skew_group的参数,如skew_group下latency最长是多少;get_ccopt_clock_tree_source_groups:获取clock tree的source group名字;

芯冰乐 280

dom4j读写xml文件方法和具体实现代码(提供jar包下载)

dom4j读写xml文件以及dom4j的jar包下载

李永昌的博客 1201

一步一步教你如何安装IP-Guard(客户端WINDOWS篇)

1、安装完服务器后,在服务器右下角有一个IP-GUARD图标:如图 2、点图标右键选注册: 3、注册成功后, 4、在开始菜单里选生成客户端安装包。 5、生成客户端安装包成功,安装名字可以任意,但是最好是英文名字。 6、将客户端安装程序复制到桌面。 7、退出所有杀毒软件、安全卫士、电脑管家等然后运行 8、安装完成后,立即重启计算机。IPguard客户端安装完成。 9、这样在控制里就可以看到已经安装客户端的计算机,在线显示蓝色,灰色表示计算机已经离线货关机 ...

IP-Guard专栏 5622

dom4j解析xml

1、Android studio导入Dom4j,implementation 'org.dom4j:dom4j:2.1.1' 2、列子如下,注意红色部分: public static void saveInstallOrDevInfoState( int installState, int devInfoState, ...

longtian635241的专栏 366

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

dom4j的.jar文件简介、下载、作用和使用方法

dom4j的.jar文件简介、下载、作用和使用方法一、dom4j引用百度百科的简介1、dom4j引用百度百科的简介2、简介总结二、dom4j的jar包怎么下载1、dom4j官网下载2、有时候官网不好用,我直接把jar放在我的网盘上了三、dom4j怎么使用1、使用idea创建一个web工程2、创建一个book的实体类 一、dom4j引用百度百科的简介 1、dom4j引用百度百科的简介 dom4j是一个Java的XML API,是jdom的升级品,用来读写XML文件的。dom4j是一个十分优秀的JavaXML

qq_37133255的博客 7762

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

dom4j 完成学生的CRUD

以前有学员到外面公司面试,公司要求他做一个CRUD的应用,条件是没有关系数据库,只容许用xml文件来做数据存储媒介,他觉得非常难,就没做出来,结果错过了一个很好的机会。后来我让他学习一下dom4j,他只用了半天的时间,CRUD就跑出来了,害他悔恨不已:早知道这东西这么简单,培训的时候真该多研究下的。那么,什么是dom4j?该如何使用它呢? dom4j是一种解析X...

逻辑无道 43

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

dom4j解析xml文件

文章目录介绍使用方法 介绍 dom4j是一个Java的XML API,是jdom的升级品,用来读写XML文件的。dom4j是一个十分优秀的JavaXML API,具有性能优异、功能强大和极其易使用的特点,它的性能超过sun公司官方的dom技术,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。在IBM developerWorks上面还可以找到一篇文章,对主流的Java XML...

LTC_1234的博客 224

关于dom4j在maven中的使用

在maven中添加dom4j的依赖, (如何使用eclipse构建maven项目, 这里就不在赘述) <!-- https://mvnrepository.com/artifact/org.dom4j/dom4j --> <dependency> <groupId>org.dom4j</gr...

weixin_30343157的博客 2616

关于Java中XML第三方解析框架DOM4J笔记

第三方解析xml文件jar包

Smoke_fog的博客 703

基于dom4j实现xml解析为map的工具类

JAVA将xml内容解析为map集合

逗神的博客 675

java调用dom4j读写xml文件

java调用dom4j读写xml文件maven引入dom4jxml文件读取写入 maven引入dom4j <!--xml读写--> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>${dom4j.version}</version> </dependency> xml文

ChuChuChuZzz的博客 216

DOM4J 实现对XML文档的增、删、改、查

前言:首先谈一个小故事:当年Java准备做对XML的解析时,对解析器的实现方向在内部发生了争执,后来高层没有听从工程师建议,坚持开发出了JDOM,而主要的工程师选择离开Java 按照自己的方式实现,就是DOM4J 。后来结果表明,DOM4J 完胜了JDOM。下面,让我们来了解一下通过DOM4J 实现对XML文件进行增删改查的过程。 1、待解析的XML文件: <students xmln

胡根得 天行健,君子以自强不息。 6565

j2ee11:xml02:dom4j解析

第一:基本概念 1.第三种解析方式不是java官方提供的,而是第三方提供的,因此需要引入外部jar包。因此dom4j要用的系统类都在外部jar包中,需要引入(前两种解析都是java官方的,系统类都在JRE System Libaray中)2.解析的本质就是找到xml文件里面的内容饿锤biu的3.dom4j的含义是从dom for java 意思是从dom解析到java。dom解析把空格也当成...

飞天小神猪 181

dom4j 和w3c document 的相互转换

<br />/**            *   org.w3c.dom.Document   ->   org.dom4j.Document            *   @param   doc   Document(org.w3c.dom.Document)            *   @return   Document            */           public   static   Document   parse(org.w3c.dom.Document   doc)   

gl7788521的专栏 898

Dom4j解析xml

package com.test;import java.io.FileWriter;import java.io.IOException;import java.util.Iterator;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentException;

Francis.Hu--技术的巅峰 647

使用dom4j操作xml实现增删改查

dom4j提供了一套操作xml文档的API,下面我们就实现一下最基本的增删改查操作。        先看下最开始的xml文档        诗展 1 22 嘿嘿 2 23   1:查找,dom4j不能直接查找某个标签,不像html dom操作一样有getElementById之类的

shizhan1881的专栏 3179
上一篇: JUnit中断言用法
下一篇: Navicat修改MySQL数据库中数据
icankeep
博客等级 码龄9年 11粉丝 76原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值