摘要:Dom4j是一个用来读写xml的工具,它是用java写的,读写性能都很优异,使用起来也很容易。
<!-- https:
//mvnrepository.com/artifact/dom4j/dom4j -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>
1.6
.
1
</version>
</dependency>
Dom4j使用过程的抛出异常:
Exception in thread
"main"
java.lang.NoClassDefFoundError: org/jaxen/JaxenException
at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:
230
)
at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:
207
)
at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:
164
)
at xmlparser.LevelsExtractor.findI(LevelsExtractor.java:
73
)
原因是Dom4j需要依赖jaxen.jar包,通过maven加入jaxven.jar即可:
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>
1.1
.
1
</version>
</dependency>
下面将讲解Dom4j的一些基本用法,由于很多例子都依赖于sea.xml文件,sea.xml文件代码如下:
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<sea>
<paths>
<path id=
"cacheConfig"
value=
"redis.xml"
></path>
<path id=
"stateConfig"
value=
"state.properties"
></path>
</paths>
<ioc>
<packages>
<
package
>com.jdk.*</
package
>
<
package
>com.ent.*</
package
>
<
package
>com.html.*</
package
>
</packages>
<beans>
<bean id=
"liveService"
class
=
"com.ent.service.LiveService"
>
<method name=
"init"
>
<param value=
"5"
></param>
</method>
</bean>
<bean id=
"testService1"
class
=
"com.ent.service.TestService"
>
</bean>
<bean id=
"testService1"
class
=
"com.ent.service.TestService"
>
</bean>
</beans>
</ioc>
</sea>
1、获取Document对象,有三种方式,如下所示:
//读取XML文件,获得document对象
SAXReader reader =
new
SAXReader();
Document document = reader.read(
new
File(
"sea.xml"
));
//解析XML形式的文本,得到document对象.
String text =
"<sea></sea>"
;
Document document = DocumentHelper.parseText(text);
//主动创建document对象.
Document document = DocumentHelper.createDocument();
//创建根节点
Element root = document.addElement(
"ioc"
);
Document对象代表整个xml文件,一般操作xml文件都需要首先获取Document对象。
2、操作节点对象的方法,如下所示:
//获取文档的根节点
Element root = document.getRootElement();
//取得某个节点的子节点
Element element=node.element(
"ioc"
);
//取得节点的文字
String text=node.getText();
//取得某节点下所有名为"ioc"的子节点,并进行遍历
List nodes = rootElm.elements(
"ioc"
);
for
(Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
// do something
}
//遍历子节点
for
(Iterator it=root.elementIterator();it.hasNext();){
Element element = (Element) it.next();
// do something
}
//添加子节点
Element elm = newElm.addElement(
"ioc"
);
//设置节点文字
elm.setText(
"com.ent.service.*"
);
//删除某节点,childElement是待删除的节点,parentElement是其父节点
parentElement.remove(childElment);
//添加一个CDATA节点
Element contentElm = infoElm.addElement(
"content"
);
contentElm.addCDATA(
"cdata区域"
);
3、操作节点属性的方法,如下所示:
//取得某节点下的某属性
Element root=document.getRootElement();
Attribute attribute=root.attribute(
"id"
);
//取得属性的文字
String text=attribute.getText();
//删除某属性
Attribute attribute=root.attribute(
"id"
);
root.remove(attribute);
//遍历某节点的所有属性
Element root=document.getRootElement();
for
(Iterator it=root.attributeIterator();it.hasNext();){
Attribute attribute = (Attribute) it.next();
String text=attribute.getText();
System.out.println(text);
}
//设置某节点的属性和文字
newMemberElm.addAttribute(
"id"
,
"liveService"
);
//设置属性的文字
Attribute attribute=root.attribute(
"id"
);
attribute.setText(
"liveService"
);
4、将文档写入xml文件,如下所示:
//文档中全为英文,不设置编码,直接写入的形式
XMLWriter writer =
new
XMLWriter(
new
FileWriter(
"sea.xml"
));
writer.write(document);
writer.close();
//文档中含有中文,设置编码格式写入的形式
//创建文件输出的时候,自动缩进的格式
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding(
"UTF-8"
);
//设置编码
XMLWriter writer =
new
XMLWriter(newFileWriter(
"sea.xml"
),format);
writer.write(document);
writer.close();
5、xml与字符串互换,如下所示:
//将字符串转化为XML
String text =
"<sea><ioc><packages><package>com.ent.*"
+
"</package></packages></ioc></sea>"
;
Document document = DocumentHelper.parseText(text);
//将文档或节点的XML转化为字符串.
SAXReader reader =
new
SAXReader();
Document document = reader.read(
new
File(
"sea.xml"
));
Element root=document.getRootElement();
String docXmlText=document.asXML();
String rootXmlText=root.asXML();
Element memberElm=root.element(
"sea"
);
String memberXmlText=memberElm.asXML();