【0】README
1)本文译自http://dom4j.sourceforge.net/dom4j-1.6.1/cookbook.html 
2)intro: 
2.1)dom4j 是一个对象模型,在内存中表示一颗XML 树。dom4j 提供了易于使用的API以提供强大的处理特性,操纵或控制 XML 和 结合 XPath, XSLT 以及 SAX, JAXP 和 DOM 来进行处理;
2.2)dom4j 是基于接口来设计的,来提供高可配置的实现策略。你只需提供一个DocumentFactory的实现就可以创建你自己的XML树实现。这使得我们易于重用dom4j 的代码,当扩展dom4j来提供所需特性的实现的时候;
【1】读取XML 数据
1)intro:dom4j 附带了一组builder 类用于解析xml 数据和创建 类似于树的对象结构。读取XML 数据的代码如下:
public class DeployFileLoaderSample {/** dom4j object model representation of a xml document. Note: We use the interface(!) not its implementation */private Document doc;/*** Loads a document from a file.* @param aFile the data source* @throw a org.dom4j.DocumentExcepiton occurs on parsing failure.*/public void parseWithSAX(File aFile) throws DocumentException {SAXReader xmlReader = new SAXReader();this.doc = xmlReader.read(aFile);}/*** Loads a document from a file.* @param aURL the data source* @throw a org.dom4j.DocumentExcepiton occurs on parsing failure.*/public void parseWithSAX(URL aURL) throws DocumentException {SAXReader xmlReader = new SAXReader();this.doc = xmlReader.read(aURL);}public Document getDoc() {return doc;}
}
2)以上代码 阐明了使用 SAXReader根据给定文件 来创建一个完整dom4j 树。org.dom4j.io 包 包含了一组类用于创建和序列化XML对象。其中read() 方法被重载了使得你能够传递表示不同资源的对象;
java.lang.String - a SystemId is a String that contains a URI e.g. a URL to a XML file
java.net.URL - represents a Uniform Resource Loader or a Uniform Resource Identifier. Encapsulates a URL.
java.io.InputStream - an open input stream that transports xml data
java.io.Reader - more compatible. Has abilitiy to specify encoding scheme
org.sax.InputSource - a single input source for a XML entity.

2.1)添加新方法为 为 DeployFileCreator  增加更多的扩展性,代码还是上面那个代码;

3)测试用例如下

@Testpublic void readXML() {String base = System.getProperty("user.dir") + File.separator+ "src" + File.separator;DeployFileLoaderSample sample = new DeployFileLoaderSample();try { // via parameter of URL type.sample.parseWithSAX(new URL("file:" + base + "pom.xml"));Document doc = sample.getDoc();System.out.println(doc.asXML());} catch (Exception e) {e.printStackTrace();}try { // via parameter of File type.sample.parseWithSAX(new File(base + "pom.xml"));Document doc = sample.getDoc();System.out.println(doc.asXML());} catch (Exception e) {e.printStackTrace();}}

【2】dom4j 和 其他XML API 整合

1)intro:dom4j 也提供了类用于和两个原始 XML 处理API(SAX 和 DOM) 进行整合。

2)DomReader类: 允许你将一个存在的 DOM 树 转换为 dom4j 树。你也可以 转换一个DOM 文档,DOM 节点分支 和 单个元素;代码如下:

public class DOMIntegratorSample {public DOMIntegratorSample() {}public org.w3c.dom.Document parse(URL url) {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();try {DocumentBuilder builder = factory.newDocumentBuilder();return builder.parse(url.toString());} catch (Exception e) {e.printStackTrace();return null;}}/** converts a W3C DOM document into a dom4j document */public Document buildDocment(org.w3c.dom.Document domDocument) {DOMReader xmlReader = new DOMReader();return xmlReader.read(domDocument);}
}public String base = System.getProperty("user.dir") + File.separator+ "src" + File.separator;@Test // 测试用例,.public void testIntegrate() {DOMIntegratorSample sample = new DOMIntegratorSample();try {org.w3c.dom.Document doc = sample.parse(new URL("file:"+ base + "pom.xml"));Document doc4j  = sample.buildDocment(doc);System.out.println(doc4j.asXML());} catch (Exception e) {e.printStackTrace();}}

【3】DocumentFactory 的秘密

1)intro: 从头到尾创建一个 Document,代码如下:

public class GranuatedDeployFileCreator {private DocumentFactory factory;private Document doc;public GranuatedDeployFileCreator() {this.factory = DocumentFactory.getInstance(); // 单例方法.}public void generateDoc(String aRootElement) {doc = factory.createDocument();Element root = doc.addElement(aRootElement);}
}

1.1)测试用例如下:

@Testpublic void testGenerateDoc() {GranuatedDeployFileCreator creator = new GranuatedDeployFileCreator();creator.generateDoc("project");Document doc = creator.getDoc();System.out.println(doc.asXML());}

2)Document 和 Element 接口有许多 助手方法以简单的方式来创动态建 XML 文档;

public class Foo {public Foo() {}public Document createDocument() {Document document = DocumentHelper.createDocument();Element root = document.addElement("root");Element author2 = root.addElement("author").addAttribute("name", "Toby").addAttribute("location", "Germany").addText("Tobias Rademacher");Element author1 = root.addElement("author").addAttribute("name", "James").addAttribute("location", "UK").addText("James Strachan");return document;}
}

2.1)测试用例如下:

@Testpublic void testCreateDocByHelper() {Foo foo = new Foo();Document doc = foo.createDocument();System.out.println(doc.asXML());}

2.2)dom4j 是基于API 的接口。这意味着dom4j中的  DocumentFactory 和 阅读器类 总是使用 org.dom4j 接口而不是其实现类。 集合 API 和 W3C 的DOM 也采用了这种 方式;

2.3)一旦你解析后创建了一个文档,你就想要将其序列化到硬盘或普通流中。dom4j 提供了一组类以以下四种方式 来序列化 你的 dom4j 树; XML + HTML + DOM + SAX Events;

【4】序列化到XML

1)intro: 使用 XMLWriter 构造器根据给定的字符编码 来传递 输出流。相比于输出流,Writer 更容易使用,因为Writer 是基于字符串的,因此有很少的编码问题。Writer.write()方法 被重写了,你可以按需逐个写出dom4j对象;

2)代码如下:

// 序列化xml
public class DeployFileCreator3 {private Document doc;public DeployFileCreator3(Document doc) {this.doc = doc;}public void serializetoXML(OutputStream out, String aEncodingScheme) throws Exception {OutputFormat outformat = OutputFormat.createPrettyPrint();outformat.setEncoding(aEncodingScheme);XMLWriter writer = new XMLWriter(out, outformat);writer.write(this.doc);writer.flush();writer.close();}}

3)测试用例

@Testpublic void testSerializetoXML() {Foo foo = new Foo();Document doc = foo.createDocument();DeployFileCreator3 creator = new DeployFileCreator3(doc);try {creator.serializetoXML(new FileOutputStream(base + "serializable.xml"),"UTF-8");System.out.println("serializable successfully.");} catch (Exception e) {e.printStackTrace();}}
<?xml version="1.0" encoding="UTF-8"?>
<root><author name="Toby" location="Germany">Tobias Rademacher</author><author name="James" location="UK">James Strachan</author>
</root>

【4.1】自定义输出格式 

1)intro:即是说,你可以定义xml的输出格式(aEncodingScheme)

 // customize output format.
public class DeployFileCreator4 {private Document doc;private OutputFormat outFormat;public DeployFileCreator4(Document doc) {this.outFormat = OutputFormat.createPrettyPrint();this.doc = doc;}public DeployFileCreator4(Document doc, OutputFormat outFormat) {this.doc = doc;this.outFormat = outFormat;}public void writeAsXML(OutputStream out) throws Exception {XMLWriter writer = new XMLWriter(out, this.outFormat);writer.write(this.doc);}public void writeAsXML(OutputStream out, String encoding) throws Exception {this.outFormat.setEncoding(encoding);this.writeAsXML(out);}
}

2)OutputFormat中一个有趣的特性是 能够设置字符编码。使用这种机制设置XMLWriter的编码方式是一个好习惯,使用这种编码方式创建OutputStream 和 输出XML的声明。

3)测试用例:

@Testpublic void testCustomizeOutputFormat() {Foo foo = new Foo();Document doc = foo.createDocument();OutputFormat format = OutputFormat.createCompactFormat();format.setEncoding("UTF-8");DeployFileCreator4 creator = new DeployFileCreator4(doc, format);try {creator.writeAsXML(new FileOutputStream(base + "customizeFormat.xml"));System.out.println("successful customize format");} catch (Exception e) {e.printStackTrace();}}
<?xml version="1.0" encoding="UTF-8"?>
<root><author name="Toby" location="Germany">Tobias Rademacher</author><author name="James" location="UK">James Strachan</author></root>

【5】打印HTML

1)intro:HTMLWriter 带有一个dom4j 树 且会将该树 格式化为 HMTL流。这个格式化器 类似于 XMLWriter 但输出的是 CDATA 和 实体区域而不是 序列化格式的XML,且它支持许多没有结束标签的HTML 元素。如<br>;

2)代码如下:

public class PrintHTML {private Document doc;private OutputFormat outFormat;public PrintHTML(Document doc) {this.outFormat = OutputFormat.createPrettyPrint();this.doc = doc;}public PrintHTML(Document doc, OutputFormat outFormat) {this.doc = doc;this.outFormat = outFormat;}public void writeAsHTML(OutputStream out) throws Exception {HTMLWriter writer = new HTMLWriter(out, this.outFormat);writer.write(this.doc);writer.flush();}
}

3)测试用例:

@Testpublic void testPrintHTML() {Foo foo = new Foo();Document doc = foo.createDocument();PrintHTML creator = new PrintHTML(doc);try {creator.writeAsHTML(new FileOutputStream(base + "printHtml.html"));System.out.println("PrintHTML successfully");} catch (Exception e) {e.printStackTrace();}}

dom4j-cookbook相关推荐

  1. 利用dom4j将实体类转换为对应的xml报文

    利用dom4j生成xml报文 目标格式: <?xml version="1.0" encoding="GBK"?><Packet type=& ...

  2. 日常记录-Pandas Cookbook

    Cookbook 1.更新内容 2.关于安装 3.Pandas使用注意事项 4.包环境 5.10分钟Pandas初识 6.教程 7.Cookbook 8.数据结构简介 9.基本功能 10.使用文本数据 ...

  3. 使用Dom4j操作XML数据

    --------------siwuxie095 dom4j 是一个非常优秀的 Java XML 的 API, 用来读写 XML 文件 和操作 XML 数据 特点:性能优异.功能强大.极端易用 dom ...

  4. 一、PyTorch Cookbook(常用代码合集)

    PyTorch Cookbook(常用代码合集) 原文链接:https://mp.weixin.qq.com/s/7at6y2NcYaxGGN8syxlccA 谢谢作者的付出.

  5. 使用dom4j解析XML例子

    包括三个文件:studentInfo.xml(待解析的xml文件), Dom4jReadExmple.java(解析的主要类), TestDom4jReadExmple.java(测试解析的结果) 代 ...

  6. dom4j读写xml文件

    dom4j读写xml文件 首先我们给出一段示例程序: import java.io.File; import java.io.FileWriter; import java.util.Iterator ...

  7. 分享一个python cookbook的在线教程地址

    分享一个python cookbook的在线教程地址: http://python3-cookbook.readthedocs.org/zh_CN/latest/ 翻译者:熊能 转载于:https:/ ...

  8. Java XML解析工具 dom4j介绍及使用实例

    Java XML解析工具 dom4j介绍及使用实例 dom4j介绍 dom4j的项目地址:http://sourceforge.net/projects/dom4j/?source=directory ...

  9. 《Python Cookbook》 最佳译本开放下载啦!

     摘要 看过<流畅的Python>的小伙伴应该知道作者Luciano Ramalho会推荐给读者另一部与Python相关的大作. David Beazley的<Python Cook ...

  10. JavaWeb学习笔记——DOM4J

    下载的地址为:http://www.dom4j.org/dom4j-1.6.1/ import java.io.File; import java.io.FileOutputStream; impor ...

最新文章

  1. 【双11狂欢的背后】微服务注册中心如何承载大型系统的千万级访问?
  2. Android 编程下代码之(QQ消息列表滑动删除)
  3. 计算机故障报告怎么写,计算机这样的诊断报告是否正常
  4. RegConnectRegistry函数
  5. Docker最全教程之使用Tencent Hub来完成CI(十)
  6. 881.BoatstoSavePeople
  7. MFC m_pMainWnd
  8. 服务器的组件中支持冗余的包括,服务器冗余电源的作用
  9. DAY9:尚学堂高琪JAVA(98)
  10. 我知道的几个免费的API数据接口
  11. 【云原生kubernetes】coredns解析集群外部域名
  12. 山东省第五届ACM大学生程序设计竞赛 Weighted Median
  13. 无法删除文件夹,目录不是空的或U盘文件无法删除的解决办法
  14. 和讯网债券数据Python爬取保存成CSV文件之一
  15. 中兴NFC手机无法读应用问题
  16. 诺基亚Lumia920
  17. 利用pandas整理订单,并根据地址对比进行分类
  18. 【OpenAirInterface】分立部署核心网与容器化ueransim仿真基站
  19. ios中位置权限_iOS 13中的位置权限
  20. seo优化之网页排版

热门文章

  1. 【ZJOI2019】线段树【线段树上dp】【大讨论】
  2. 【十二省联考】春节十二响【贪心】【堆】【启发式合并】
  3. 2021牛客暑期多校训练营5
  4. P3389 【模板】高斯消元法
  5. 牛客题霸 [容器盛水问题] C++题解/答案
  6. 一起开心2020暑假训练第一周
  7. 专题突破一之分块——Untitled Problem II,Balanced Lineup,[ioi2009]Regions
  8. YbtOJ#20066-[NOIP2020模拟赛B组Day4]筹备计划【线段树,树状数组】
  9. P3327-[SDOI2015]约数个数和【莫比乌斯反演】
  10. P3901-数列找不同【模拟】