dom4j解析xml
来源:互联网 发布:vscode markdown html 编辑:程序博客网 时间:2024/06/03 20:56
Dom4j解析XML
1. 基本用法
参考dom4j使用.
2. 解析方式
2.1 DOM
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = fact.newDocumentBuilder();
Document doc_xml = builder.parse(new File(“test.xml”));
DOMReader reader_d = new DOMReader(new DOMDocumentFactory()
);
org.dom4j.dom.DOMDocument doc = (DOMDocument) reader_d.read(doc_xml);
2.2 SAX
SAXReader reader = new SAXReader();
org.dom4j.Document doc = reader.read(new File(“test.xml”));
SAX解析器不像DOM那样建立一个完整的文档树,而是在读取文档时激活一系列事件,这些事件被推给事件处理器,然后由事件处理器提供对文档内容的访问。那些只需要单遍读取内容的应用程序可以从SAX解析中大大受益。而如果要修改和操作XML,则很少使用SAX。
注意
SAXReade r sr = new SAXReader();
sr.setDocumentFactory(new DOMDocumentFactory());
try {
DOMDocument doc = (DOMDocument) sr.read(xmlstring);
}
通过SAXReader也可以创建出DOMDocument,从上面的代码我们可以看出创建dom4j文档树跟采用的DocumentFactory有关
3. DocumentFactory
源码中的介绍:
/*
* DocumentFactory
is a collection of factory methods to allow
* easy custom building of DOM4J trees. The default tree that is built uses a
* doubly linked tree.
* The tree built allows full XPath expressions from anywhere on the tree.
*/
当使用的是DOMDocumentFactory时,创建的节点是DOMElement实例,使用默认的DocumentFactory时,则创建的Dom4j节点树节点都是AbstractElement实例。从Dom4j API中知DOMElement是 org.dom4j.tree.AbstractElement的子类,但是当创建SAXReader或者DOMReader不指定DocumentFactory时,不能将Document转换为DOMDocument.
由于采用的DocumentFactory不同,将直接影响XML元素在内存中的存储结构,导致检索节点是性能有所差异,可以参看http://www.iteye.com/topic/1117606做的测试。
Dom4j中支持的DocumentFactory类型有:
Written with StackEdit.
- 用DOM4J解析XML
- 使用 dom4j 解析 XML
- 使用 dom4j 解析 XML
- 解析xml之--DOM4J
- dom4j解析XML
- 使用 dom4j 解析 XML
- xml文件解析-DOM4J
- 使用 dom4j 解析 XML
- 使用 dom4j 解析 XML
- dom4j解析xml方法
- 使用 dom4j 解析 XML
- 使用 dom4j 解析 XML
- 使用 dom4j 解析 XML
- 使用 dom4j 解析 XML
- 使用 dom4j 解析 XML
- 使用 dom4j 解析 XML
- 使用 dom4j 解析 XML
- 使用 dom4j 解析 XML
- POJ-3279-Fliptile
- PHP对程序员的要求更高
- Pat(Advanced Level)Practice--1077(Kuchiguse)
- muduo源码分析--事件回调层次是怎么传递的Tcpserver Channel TcpConnection
- 最小生成树 prim kruscal
- dom4j解析xml
- Nsight 调试出现Parallel Nsight Debug CUDA grid launch failed: CUcontext: 211577632 CUmodule: 223878456
- 利用着色器实现游戏的公告板特效
- 中国象棋将帅问题----《编程之美》读书笔记
- 文件复制的C语言实现
- QT for IOS 模拟机运行
- 1114班第三周作业,归并排序
- 黑马程序员 高新技术<二>静态导入,自动装拆箱,增强for循环等
- hdu 4267 the Ghost Blows Light