package com.itjob.xml;

import java.io.File;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*; 

/**
 * 第7章 Java XML编程
 * 考题：使用DOM4J解析7.2.2章节中的students.xml文件，打印每个student元素的name、age、address和phone的文本值
 * @author AmigoXie
 */
public class DOM4J { 
	public static void main(String arge[]) {
		long startTime = System.currentTimeMillis();
		try { 
		
			File f = new File("D:\\project\\questionbank\\students.xml"); 
			SAXReader reader = new SAXReader(); 
			Document doc = reader.read(f); 
			Element root = doc.getRootElement(); 
			Element foo; 
			for (Iterator i = root.elementIterator("student"); i.hasNext();) { 
				foo = (Element) i.next(); 
				System.out.println("name=" + foo.elementText("name")); 
				System.out.println("age=" + foo.elementText("age"));
				System.out.println("address=" + foo.elementText("address"));
				System.out.println("phone=" + foo.elementText("phone"));
				System.out.println();
			} 
		} catch (Exception e) { 
			e.printStackTrace(); 
		}
		long endTime = System.currentTimeMillis();
		System.out.println(endTime - startTime);
	}
}

