概述
感谢原作者,本文参考或转载自以下地址:
使用 dom4j 解析 XML http://www.ibm.com/developerworks/cn/xml/x-dom4j.html#ibm-pcon
Dom4j遍历XML http://www.2cto.com/kf/201211/166644.html
Dom4j应用总结 http://lavasoft.blog.51cto.com/62575/78450/
包
使用 import 语句导入 dom4j API 类:
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
创建文档实例 Document
使用DocumentHelper
类创建一个文档实例。
DocumentHelper
是生成 XML 文档节点的 dom4j API 工厂类。
Document document = DocumentHelper.createDocument();
创建文档实例2 Document
使用SAXReader。
Document document =
new SAXReader().read(new File(directory.getAbsolutePath() + "/catalog.xml"));
使用 addElement()
方法创建根元素 xx
。 addElement()
用于向 XML 文档中增加元素。
Element catalogElement = document.addElement("catalog");
获取根元素 RootElement
使用getRootElement()方法获取根元素xx。
Element root = document.getRootElement();
创建元素 Element
在xx 元素中使用 addElement()
方法增加 xx
元素。
Element journalElement =
catalogElement.addElement("journal");
添加注释 Comment
在xx元素中使用 addComment()
方法添加注释“xxxxxxxx”。
catalogElement.addComment("An XML catalog");
@see http://www.ibm.com/developerworks/cn/xml/x-wxxm/part3/
@see http://msdn.microsoft.com/zh-cn/library/ms256173(v=VS.80).aspx
添加属性 Attribute
使用 addAttribute()
方法向 xx
元素添加xxx 和 xxx
属性。
journalElement.addAttribute("title", "XML Zone");
journalElement.addAttribute("publisher", "IBM developerWorks");
设置文本 Text
使用setText()
方法设置
xx
元素的文本。
titleElement.setText("Java configuration with XML Schema");
创建并写入XML
注意添加异常的捕获与处理 *
try{
XMLWriter output = new XMLWriter(
new FileWriter( new File("c:/catalog/catalog.xml") ));
output.write( document );
output.close();
}
catch(IOException e){System.out.println(e.getMessage());}
解析文档 XML
使用 SAXReader 解析 XML 文档 x.xml。**
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXml);
//inputXml 是从 c:/catalog/catalog.xml 创建的 java.io.File
修改属性 Attribute
使用 XPath 表达式从xx 元素中获得 xxx 节点列表。如果 xxx 属性值是“xxxxxxx”则改为“yyyyyyy”。
List list = document.selectNodes("//article/@level" );
Iterator iter=list.iterator();
while(iter.hasNext()){
Attribute attribute=(Attribute)iter.next();
if(attribute.getValue().equals("Intermediate"))
attribute.setValue("Introductory");
}
修改元素文本 Text
获取xx 元素列表,从 xx 元素中的 xxx 元素得到一个迭代器,并修改xx 元素的文本。
list = document.selectNodes("//article" );
iter=list.iterator(); //迭代器
while(iter.hasNext()){
Element element=(Element)iter.next();
Iterator iterator=element.elementIterator("title");
while(iterator.hasNext()){
Element titleElement=(Element)iterator.next();
if(titleElement.getText().equals("Java configuration with XML Schema"))
titleElement.setText("Create flexible and extensible XML schema");
}
}
遍历xml文档 xml
使用递归方法遍历整个xml文档获取所有元素***
private static void getElement(Element element) {
List list = element.elements();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Element chileEle = (Element) iterator.next();
String blank = "";
int j = Integer.parseInt(chileEle.attributeValue("level"));
for (int i = 0; i < j; i++) {
blank = blank + " | ";
}
System.out.println(blank + "(" + j + ")"
+ chileEle.getName() + ": "
+ chileEle.getText().replace("n", ""));
getElement(chileEle);
}
*尚未学习“异常”
**SAXReader
包含在 org.dom4j.io 包中
***效果见清单5、6、7
--------------------------------------------------------------------------------------------------------------------------------------
清单 1. 示例 XML 文档(catalog.xml)
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<!--An XML Catalog-->
<?target instruction?>
<journal title="XML Zone"
publisher="IBM developerWorks">
<article level="Intermediate" date="December-2001">
<title>Java configuration with XML Schema</title>
<author>
<firstname>Marcello</firstname>
<lastname>Vitaletti</lastname>
</author>
</article>
</journal>
</catalog>
清单 2. 修改后的 XML 文档(catalog-modified.xml)
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<!--An XML catalog-->
<?target instruction?>
<journal title="XML Zone"
publisher="IBM developerWorks">
<article level="Introductory" date="October-2002">
<title>Create flexible and extensible XML schemas</title>
<author>
<firstname>Ayesha</firstname>
<lastname>Malik</lastname>
</author>
</article>
</journal>
</catalog>
清单 3. 生成 XML 文档 catalog.xml 的程序(XmlDom4J.java)
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import java.io.*;
public class XmlDom4J{
public void generateDocument(){
Document document = DocumentHelper.createDocument();
Element catalogElement = document.addElement("catalog");
catalogElement.addComment("An XML Catalog");
catalogElement.addProcessingInstruction("target","text");
Element journalElement =
catalogElement.addElement("journal");
journalElement.addAttribute("title", "XML Zone");
journalElement.addAttribute("publisher", "IBM developerWorks");
Element articleElement=journalElement.addElement("article");
articleElement.addAttribute("level", "Intermediate");
articleElement.addAttribute("date", "December-2001");
Element
titleElement=articleElement.addElement("title");
titleElement.setText("Java configuration with XML Schema");
Element authorElement=articleElement.addElement("author");
Element
firstNameElement=authorElement.addElement("firstname");
firstNameElement.setText("Marcello");
Element lastNameElement=authorElement.addElement("lastname");
lastNameElement.setText("Vitaletti");
document.addDocType("catalog",
null,"file://c:/Dtds/catalog.dtd");
try{
XMLWriter output = new XMLWriter(
new FileWriter( new File("c:/catalog/catalog.xml") ));
output.write( document );
output.close();
}
catch(IOException e){System.out.println(e.getMessage());}
}
public static void main(String[] argv){
XmlDom4J dom4j=new XmlDom4J();
dom4j.generateDocument();
}}
清单 4. 用于修改 catalog.xml 的程序(Dom4Jparser.java)
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Attribute;
import java.util.List;
import java.util.Iterator;
import org.dom4j.io.XMLWriter;
import java.io.*;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public class Dom4JParser{
public void modifyDocument(File inputXml){
try{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXml);
List list = document.selectNodes("//article/@level" );
Iterator iter=list.iterator();
while(iter.hasNext()){
Attribute attribute=(Attribute)iter.next();
if(attribute.getValue().equals("Intermediate"))
attribute.setValue("Introductory");
}
list = document.selectNodes("//article/@date" );
iter=list.iterator();
while(iter.hasNext()){
Attribute attribute=(Attribute)iter.next();
if(attribute.getValue().equals("December-2001"))
attribute.setValue("October-2002");
}
list = document.selectNodes("//article" );
iter=list.iterator();
while(iter.hasNext()){
Element element=(Element)iter.next();
Iterator iterator=element.elementIterator("title");
while(iterator.hasNext()){
Element titleElement=(Element)iterator.next();
if(titleElement.getText().equals("Java configuration with XML
Schema"))
titleElement.setText("Create flexible and extensible XML schema");
}
}
list = document.selectNodes("//article/author" );
iter=list.iterator();
while(iter.hasNext()){
Element element=(Element)iter.next();
Iterator iterator=element.elementIterator("firstname");
while(iterator.hasNext()){
Element firstNameElement=(Element)iterator.next();
if(firstNameElement.getText().equals("Marcello"))
firstNameElement.setText("Ayesha");
}
}
list = document.selectNodes("//article/author" );
iter=list.iterator();
while(iter.hasNext()){
Element element=(Element)iter.next();
Iterator iterator=element.elementIterator("lastname");
while(iterator.hasNext()){
Element lastNameElement=(Element)iterator.next();
if(lastNameElement.getText().equals("Vitaletti"))
lastNameElement.setText("Malik");
}
}
XMLWriter output = new XMLWriter(
new FileWriter( new File("c:/catalog/catalog-modified.xml") ));
output.write( document );
output.close();
}
catch(DocumentException e)
{
System.out.println(e.getMessage());
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String[] argv){
Dom4JParser dom4jParser=new Dom4JParser();
dom4jParser.modifyDocument(new File("c:/catalog/catalog.xml"));
}
}
清单5. 新的catalog.xml文档 (附有level属性)
<?xml version="1.0" encoding="UTF-8"?>
<catalog level="0">
<!--An XML Catalog-->
<journal title="XML Zone"
publisher="IBM developerWorks" level="1">
<article level="2">
<title level="3">Java configuration with XML Schema</title>
<author level="3">
<firstname level="4">Marcello</firstname>
<lastname level="4">Vitaletti</lastname>
</author>
</article>
</journal>
<magazine level="1">
<article level="2">
<title level="3">
<old level="4">
</old>
<now level="4">
</now>
</title>
<author level="3">
<firstname level="4">
</firstname>
<lastname level="4">
</lastname>
</author>
<date level="3">
</date>
</article>
<comic level="2">
<title level="3">
</title>
<author level="3">
<firstname level="4">
</firstname>
<lastname level="4">
</lastname>
</author>
</comic>
</magazine>
</catalog>
清单6. 利用递归遍历所有元素并获取其文本
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Tww {
public static void main(String[] args) {
Document document = null;
File directory = new File("");
try {
document = new SAXReader().read(new File(directory
.getAbsolutePath() + "/catalog.xml"));
} catch (DocumentException e) {
System.out.println("!可能是非法XML文档");
e.printStackTrace();
}
Element root = document.getRootElement();
System.out.println("(0)Root:" + root.getName());
getElement(root);
}
private static void getElement(Element element) {
List list = element.elements();
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Element chileEle = (Element) iterator.next();
String blank = "";
int j = Integer.parseInt(chileEle.attributeValue("level"));
for (int i = 0; i < j; i++) {
blank = blank + " | ";
}
System.out.println(blank + "(" + j + ")"
+ chileEle.getName() + ": "
+ chileEle.getText().replace("n", ""));
getElement(chileEle);
}
}
}
清单7. 上面清单5与与清单6的效果
(0)Root:catalog
| (1)journal:
|
| (2)article:
|
|
| (3)title: Java configuration with XML Schema
|
|
| (3)author:
|
|
|
| (4)firstname: Marcello
|
|
|
| (4)lastname: Vitaletti
| (1)magazine:
|
| (2)article:
|
|
| (3)title:
|
|
|
| (4)old:
|
|
|
| (4)now:
|
|
| (3)author:
|
|
|
| (4)firstname:
|
|
|
| (4)lastname:
|
|
| (3)date:
|
| (2)comic:
|
|
| (3)title:
|
|
| (3)author:
|
|
|
| (4)firstname:
|
|
|
| (4)lastname:
最后
以上就是文艺月光为你收集整理的java使用dom4j解析xml的笔记 使用 dom4j 解析 XML http://www.ibm.com/developerworks/cn/xml/x-dom4j.html#ibm-pcon 的全部内容,希望文章能够帮你解决java使用dom4j解析xml的笔记 使用 dom4j 解析 XML http://www.ibm.com/developerworks/cn/xml/x-dom4j.html#ibm-pcon 所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复