package com.xml.dom4j;
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XMLReader {
public void read(String fileName) throws DocumentException{
File xmlFile = new File(fileName);
read(xmlFile);
}
@SuppressWarnings("unchecked")
public void read(File xmlFile) throws DocumentException{
//使用SAX方式解析xml
SAXReader saxReader = new SAXReader();
//把xml文件转换成文档对象
Document document = saxReader.read(xmlFile);
//取得根节点
Element rootNode = document.getRootElement();
//取得根节点的名字
System.out.println("根节点名称: "+rootNode.getName());
//取得所有的二级(下一级)子节点
List secondList = rootNode.elements();
readsecondLevelElement(secondList);
}
@SuppressWarnings("unchecked")
public void readsecondLevelElement( List secondList){
for(Element secondElement : secondList){
System.out.println(" \t二级节点名称:"+secondElement.getName());
//取得所有的二级子节点的属性
System.out.println(" \t 属性title:"+secondElement.attributeva lue("title")+" 属性publisher:"+secondElement.attributeva lue("publisher"));
//取得所有的三级(下一级)子节点
List thirdList = secondElement.elements();
readThirdLevelElement(thirdList);
}
}
@SuppressWarnings("unchecked")
public void readThirdLevelElement( List thirdList){
for(Element thirdElement : thirdList){
System.out.println(" \t\t三级节点名称:"+thirdElement.getName());
//取得所有的三级子节点的属性
System.out.println(" \t\t 属性level:"+thirdElement.attributeva lue("level")+" 属性date:"+thirdElement.attributeva lue("date"));
//取得所有的四级(下一级)子节点
List fourList = thirdElement.elements();
readFourLevelElement(fourList);
}
}
//读取四级元素的方法
public void readFourLevelElement( List fourList){
for(Element fourElement : fourList){
System.out.println(" \t\t\t四级节点名称:"+fourElement.getName());
if(fourElement.getName().equals("title")){
System.out.println(" \t\t\t 四级节点内容:"+fourElement.getText());
}else if(fourElement.getName().equals("author")){
//fourElement.element("firstname") 通过名字取得firstname节点
System.out.println(" \t\t\t\t五级级节点内容:"+fourElement.element("firstname").getName());
System.out.println(" \t\t\t\t 五级节点内容:"+fourElement.element("firstname").getText());
System.out.println(" \t\t\t\t五级级节点内容:"+fourElement.element("lastname").getName());
System.out.println(" \t\t\t\t 五级节点内容:"+fourElement.element("lastname").getText());
}
}
}
}
编写测试类
public void testRead() {
try {
String file = "F:/students/dom4j_XML/src/catalog.xml";
XMLReader reader = new XMLReader();
reader.read(file);
} catch (DocumentException e) {
}
}
要点说明:
使用dom4j方式解析xml:
1.指定要解析的文件
File xmlFile = new File(fileName);
2.选择xml解析方式
//使用SAX方式解析xml
SAXReader saxReader = new SAXReader();
3.解析xml文件
//把xml文件转换成文档对象
Document document = saxReader.read(xmlFile);
4.解析文档对象dom对象
//取得根节点
Element rootNode = document.getRootElement();
//取得所有的下一级节点
List secondList = rootNode.elements();
//根据名字取的子节点
fourElement.element("firstname")
//根据名字取的子节点,和节点内容
fourElement.element("firstname").getText()
//根据名字取的节点属性值
thirdElement.attributeva lue("level")
document.selectSingleNode( "/web-app/session-config/session-timeout "); //从根结点向下得到一个node