创建XML文件
1 package cn.com.util; 2 3 import java.io.File; 4 import java.util.Iterator; 5 6 import org.dom4j.Document; 7 import org.dom4j.DocumentException; 8 import org.dom4j.Element; 9 import org.dom4j.io.SAXReader; 10 11 //解析XML文件 12 public class DOM4JReader { 13 public static void main(String[] args) { 14 File file = new File("d:" + File.separator + "output.xml"); 15 SAXReader reader = new SAXReader(); 16 Document doc = null; 17 try { 18 doc = reader.read(file); 19 } catch (DocumentException e) { 20 e.printStackTrace(); 21 } 22 Element root = doc.getRootElement(); // 取得根元素 23 Iterator iter = root.elementIterator(); // 取得全部的子节点 24 while (iter.hasNext()) { 25 Element linkman = (Element) iter.next(); 26 System.out.println("姓名:" + linkman.elementText("name")); // 取得name元素的内容 27 System.out.println("邮箱:" + linkman.elementText("email"));// 取得email元素内容 28 } 29 } 30 }