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; 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")); //list本身for循环 // for(int i = 0;i<list.size();i++){ // Element chileEle = (Element)list.get(i); // System.out.println("节点:"+chileEle.getName()+",内容:"+chileEle.attributeValue("id")); // } //迭代器循环 1. // Iterator its = list.iterator(); // for(int i = 0;i<list.size();i++){ // if(its.hasNext()){ // Element chileEle = (Element)its.next(); // System.out.println("节点:"+chileEle.getName()+",内容:"+chileEle.attributeValue("id")); // } // } //迭代器循环 2. // for(Iterator its = list.iterator();its.hasNext();){ // Element chileEle = (Element)its.next(); // System.out.println("节点:"+chileEle.getName()+",内容:"+chileEle.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文件实例)