import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.tree.DefaultAttribute;
public class dom4j {
//存储xml元素信息的容器
private static List<dom_bean> elemList = new ArrayList<dom_bean>();
/*
* 这个方法是通过xml路径读取xml文件,并将其DOM结构返回
*/
public Document parse2Document(String filePath){
Document document = null ;
//创建SAXReader读取器,专门用于读取xml
SAXReader saxread = new SAXReader();
//将xml转换为输入流
try {
InputStream inputStream = new FileInputStream(new File(filePath));
document = saxread.read(inputStream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
//这个是抛出xml文件转化为输入流的异常
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
//这个抛出SAXReader读取xml转换为document树的异常
e.printStackTrace();
}
return document ;
}
/**
* 获取根元素
*
* @return
* @throws DocumentException
*/
public Element getRootElement(String xmlfilePath) throws DocumentException {
Document document = parse2Document(xmlfilePath);
Element elem = document.getRootElement();
return elem;
}
/**
* 递归遍历方法
* <功能详细描述>
* @param element
* @see [类、类#方法、类#成员]
*/
public void getElementList(Element element)
{
List elements = element.elements();
// 没有子元素
if (elements.isEmpty())
{
String xpath = element.getPath();
String value = element.getTextTrim();
elemList.add(new dom_bean(getNoteAttribute(element), xpath, value));
}
else
{
// 有子元素
Iterator it = elements.iterator();
while (it.hasNext())
{
Element elem = (Element)it.next();
// 递归遍历
getElementList(elem);
}
}
}
/**
* 获取节点所有属性值
* <功能详细描述>
* @param element
* @return
* @see [类、类#方法、类#成员]
*/
public String getNoteAttribute(Element element)
{
String xattribute = "";
DefaultAttribute e = null;
List list = element.attributes();
for (int i = 0; i < list.size(); i++)
{
e = (DefaultAttribute)list.get(i);
xattribute += " [name = " + e.getName() + ", value = " + e.getText() + "]";
}
return xattribute;
}
public String getListString(List elemList)
{
StringBuffer sb = new StringBuffer();
for (Iterator it = elemList.iterator(); it.hasNext();)
{
dom_bean leaf = (dom_bean)it.next();
sb.append("xpath: " + leaf.getXpath()).append(", value: ").append(leaf.getValue());
if (!"".equals(leaf.getXattribute()))
{
sb.append(", Attribute: ").append(leaf.getXattribute());
}
sb.append("\n");
}
return sb.toString();
}
public static void main(String args[]){
dom4j dom = new dom4j();
Element root;
String x = "";
try {
root = dom.getRootElement("dog.xml");
dom.getElementList(root);
x = dom.getListString(elemList);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("-----------解析结果------------");
System.out.println(x);
}
}
package com.dom4j;
//这个类用来存储dom的数据结构
public class dom_bean {
// 节点属性
private String xattribute;
// 节点PATH
private String xpath;
// 节点值
private String value;
public dom_bean(String xattribute, String xpath, String value)
{
this.xattribute = xattribute;
this.xpath = xpath;
this.value = value;
}
public String getXpath()
{
return xpath;
}
public void setXpath(String xpath)
{
this.xpath = xpath;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
public String getXattribute()
{
return xattribute;
}
public void setXattribute(String xattribute)
{
this.xattribute = xattribute;
}
}