package com.itjob.xml;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * 第7章 Java XML编程
 * 使用DOM解析技术解析student.xml文件
 * @author AmigoXie
 */
public class DOMXMLReader {
	public static void main(String args[]) {
		long startTime = System.currentTimeMillis();
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse("students.xml");
			doc.normalize();
			// 取得多个student元素
			NodeList links = doc.getElementsByTagName("student");
			for (int i = 0; i < links.getLength(); i++) {
				Element link = (Element) links.item(i);
				System.out.println("name=" + link.getElementsByTagName("name").item(0)
						.getFirstChild().getNodeValue());
				System.out.println("age=" + link.getElementsByTagName("age").item(0)
						.getFirstChild().getNodeValue());
				System.out.println("address=" + link.getElementsByTagName("address").item(0)
						.getFirstChild().getNodeValue());
				System.out.println("phone=" + link.getElementsByTagName("phone").item(0)
						.getFirstChild().getNodeValue());
				System.out.println();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		long endTime = System.currentTimeMillis();
		System.out.println(endTime - startTime);
	}
}
