导入jar包dom4j-1.6.1.jar。
XML文件如下所示:
<notice type="jsz">
<atts name="通知公告">
<att key="告知编号">
<dbName>str1</dbName>
</att>
<att key="姓名" dbName="str2"></att>
</atts>
</notice>
Java代码如下所示:
/**
*
*/
package com.gsoft.geloin;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* @author Geloin
*
*/
public class MyTest {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
String configFile = "c:/Users/Geloin/Desktop/test.xml";
// 将xml文件读取到Document中。
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new BufferedReader(
new InputStreamReader(
new FileInputStream(new File(configFile)), "GBK")));
// 获取根节点
Element root = document.getRootElement();
// 取根节点的属性
List<Attribute> rootAtts = root.attributes();
for (Attribute att : rootAtts) {
// 打印type===jsz
System.out.println(att.getName().trim() + "==="
+ att.getValue().trim());
}
// 取下一级子节点
Iterator<Element> seconds = root.elementIterator();
while (seconds.hasNext()) {
Element second = seconds.next();
// 取第二级的属性
List<Attribute> secondAtts = second.attributes();
for (Attribute att : secondAtts) {
// 打印name===通知公告
System.out.println(att.getName().trim() + "==="
+ att.getValue().trim());
}
// 取下一级子节点
Iterator<Element> thirds = second.elementIterator();
while (thirds.hasNext()) {
Element third = thirds.next();
// 取第二级的属性
List<Attribute> thirdAtts = third.attributes();
for (Attribute att : thirdAtts) {
// 打印key===告知编号、key===姓名、dbName===str2
System.out.println(att.getName().trim() + "==="
+ att.getValue().trim());
}
// 取第四级子节点
Iterator<Element> fours = third.elementIterator();
while (fours.hasNext()) {
Element four = fours.next();
// 打印dbName===str1
System.out.println(four.getName().trim() + "===="
+ four.getTextTrim());
}
}
}
}
}
主要方法:
(1) org.dom4j.io.SAXReader.read(Reader):将XML文件读取到Document里面,注意上述代码中的编码设置,有中文时设置为GBK,若还是乱码,则换成UTF-8;
(2) org.dom4j.Document.getRootElement():获取根节点;
(3) org.dom4j.Element.attributes():获取所有的属性;
(4) org.dom4j.Node.getName():获取属性名或节点名;
(5) org.dom4j.Attribute.getValue():获取属性值;
(6) org.dom4j.Element.getTextTrim():获取标签的内容,即<a>text</a>中的text。