step 1:获取dom解析器工厂
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
setp 2:获取dom解析器
DocumentBuilder db = dbf.newDocumentBuilders();
step 3:获取xml整个文档(根节点)
Document document = db.parse(new File("student.xml");//使用了相对路径获取xml文档
NodeList nodeList = document.getElementsByTagName("student");
for(int i = 0; i < nodeList.getLength(); i++)
{
Element element = (Element)nodeList.item(i);
String name = element.getElementsByTagName("name").item(0).getFirstChild().getNodeVale();
String sex= element.getElementsByTagName("sex").item(0).getFirstChild().getNodeVale();
String address= element.getElementsByTagName("address").item(0).getFirstChild().getNodeVale();
String age= element.getElementsByTagName("age").item(0).getFirstChild().getNodeVale();
System.out.println("姓名:"+name);
System.out.println("性别:"+sex);
System.out.println("年龄:"+age);
System.out.println("地址:"+address);
}