<?xml version="1.0" encoding="GBK"?>
<addresslist>
<name>hemmingway</name>
</addresslist>


上面是test.xml文件, 下面是DOMDemo.java文件

package org.lxh.xml.dom;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DOMDemo {
public static void main(String[] args){
//1. build DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

//2.
DocumentBuilder builder = null;
try{
builder = factory.newDocumentBuilder();
}catch(ParserConfigurationException e){
e.printStackTrace();
}

//3.
Document doc = null;
try{
doc = builder.parse("D:" + File.separator + "test.xml");
}catch(SAXException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}

//4.
NodeList nl = doc.getElementsByTagName("name");

//5.
System.out.println("姓名: " + nl.item(0).getFirstChild().getNodeValue());

}

}