Dom4j遍历XML

package read;  
  
import java.io.File;  
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;  

/**
 * @author cyrusLiu
 * @creation 2012-11-7
 */
public class ReadXML {  
  
    public static void main(String[] args) {  
        Document doc = null;  
        try {  
            doc = new SAXReader().read(new File("./WebRoot/WEB-INF/")+"//daoFacotry.xml");  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        }  
       Element root = doc.getRootElement();  
       System.out.println("根节点:"+root.getName()+",内容:"+root.attributeValue("id"));  

       getElement(root);  
          
    }  
      
    private static void getElement(Element element){  
        List list = element.elements();  
        //递归方法   
        for(Iterator its =  list.iterator();its.hasNext();){  
            Element chileEle = (Element)its.next();  
            System.out.println("节点:"+chileEle.getName()+",内容:"+chileEle.attributeValue("id"));  
            getElement(chileEle);  
        }  
    }   
}  


 

你可能感兴趣的:(工作,dom4j,二厶可可肉,递归XML,遍历XML)