java利用dom4j递归输出所有节点

test.xml文件

<?xml version="1.0" encoding="GBK"?> 
<doc> 
    <person> 
        <name>某人</name> 
        <adds>             
            <add ID="10002"> 
                <BS>10002</BS> 
                <note>南昌市</note> 
            </add> 
            <add ID=""> 
                <BS>10002</BS> 
                <note>空ID节点啊</note> 
            </add> 
            <add> 
                <BS>10002</BS> 
                <note>空ID节点啊</note> 
            </add> 
      <add ID="10001"> 
          <note>深圳市</note> 
      </add> 
  </adds> 
    </person> 
    <other> 
        <name ID="HEHE">其它节点</name> 
    </other> 
</doc> 

 Leaf.java

package com.lushuifa.dom;

public class Leaf {
	private String xpath;// 
	private String value;
	private String domName;

	public String getDomName() {
		return domName;
	}

	public void setDomName(String domName) {
		this.domName = domName;
	}

	public Leaf(String xpath, String value,String domName) {
		this.xpath = xpath;
		this.value = value;
		this.domName = domName;
	}

	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;
	}
}

 DomUtil.java文件代码

package com.lushuifa.dom;

import java.util.ArrayList;
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 DomUtil {
	
	
	public Document readXML() {  
        Document document = null;  
        SAXReader saxReader = new SAXReader();  
        try {  
            document = saxReader.read("F:\\testSpace\\Dom4jTest\\src\\test.xml");  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        }  
        return document;  
    }  
	
	 /** 
     * 获取根元素 
     * 
     * @return 
     * @throws DocumentException 
     */ 
    public Element getRootElement(){ 
        try {
			Document srcdoc = this.readXML(); 
			Element elem = srcdoc.getRootElement(); 
			return elem;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null; 
    } 

    /** 
     * 递归遍历方法 
     * 
     * @param element 
     */ 
    public List<Leaf> getElementList(Element element,ArrayList<Leaf> elemList) { 
        List elements = element.elements(); 
        if (elements.size() == 0) { 
            //没有子元素 
            String xpath = element.getPath(); 
            String value = element.getTextTrim(); 
            String domName = element.getName();
            elemList.add(new Leaf(xpath, value,domName)); 
        } else { 
            //有子元素 
            for (Iterator it = elements.iterator(); it.hasNext();) { 
                Element elem = (Element) it.next(); 
                //递归遍历 
                getElementList(elem,elemList); 
            } 
        }
        return elemList;
    }
    

    public String getListString(List<Leaf> elemList) { 
        StringBuffer sb = new StringBuffer(); 
        for (Iterator<Leaf> it = elemList.iterator(); it.hasNext();) { 
            Leaf leaf = it.next(); 
            sb.append(leaf.getXpath()).append(" = ").append(leaf.getValue()).append("\n"); 
        } 
        return sb.toString(); 
    } 
    
    public static void main(String[] args) {
    	DomUtil domUtil = new DomUtil();
    	System.out.println("得到document对象是:"+domUtil.readXML());;
    	Element rootElement =  domUtil.getRootElement();
    	List<Leaf> list = domUtil.getElementList(rootElement,new ArrayList<Leaf>());
    	System.out.println("list=="+list);
    	for (Leaf leaf : list) {
			 System.out.println("节点路径:"+leaf.getXpath());
			 System.out.println("节点名称:"+leaf.getDomName());
			 System.out.println("节点值:"+leaf.getValue());
		}
	}
} 

 测试结果:

节点路径:/doc/person/name

节点名称:name

节点值:某人

节点路径:/doc/person/adds/add/BS

节点名称:BS

节点值:10002

节点路径:/doc/person/adds/add/note

节点名称:note

节点值:南昌市

节点路径:/doc/person/adds/add/BS

节点名称:BS

节点值:10002

节点路径:/doc/person/adds/add/note

节点名称:note

节点值:空ID节点啊

节点路径:/doc/person/adds/add/BS

节点名称:BS

节点值:10002

节点路径:/doc/person/adds/add/note

节点名称:note

节点值:空ID节点啊

节点路径:/doc/person/adds/add/note

节点名称:note

节点值:深圳市

节点路径:/doc/other/name

节点名称:name

节点值:其它节点

你可能感兴趣的:(dom4j处理xml,dom4j递归遍例所有节点)