<?xml version="1.0" encoding="utf-8"?>
<students><student stuno="1001"><name>qingzhiyu</name><age>22</age><address>北京</address></student><student stuno="1002"><name>zhouxiaoqing</name><age>20</age><address>厦门</address></student>
</students>
package javaxml;public class Student {private String stuno;private String name;private String address;private String age;/*** @return the stuno*/
public String getStuno() {return stuno;
}
/*** @param stuno the stuno to set*/
public void setStuno(String stuno) {this.stuno = stuno;
}
/*** @return the name*/
public String getName() {return name;
}
/*** @param name the name to set*/
public void setName(String name) {this.name = name;
}
/*** @return the address*/
public String getAddress() {return address;
}
/*** @param address the address to set*/
public void setAddress(String address) {this.address = address;
}
/*** @return the age*/
public String getAge() {return age;
}
/*** @param age the age to set*/
public void setAge(String age) {this.age = age;
}
@Overridepublic String toString() {String str="stuno="+stuno+",name="+name+",age="+age+",address="+address;return str;}}
package javaxml;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
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;public class DOMXMLUtil {public static void main(String[] args) throws Exception {List<Student>students=new ArrayList<>();
// 对xml文件进行读取操作InputStream inputStream=DOMXMLUtil.class.getClassLoader().getResourceAsStream("student.xml");
// 实例化解析工厂DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
// 建立解析器对象DocumentBuilder builder=factory.newDocumentBuilder();
// 对xml对象进行解析并获取解析文档Document document = builder.parse(inputStream);
// 获取根节点对象Element root=document.getDocumentElement();System.out.println("根节点对象的名字:"+root.getTagName());
// 获取根节点students当中的全部名字为student的子节点对象NodeList nodeList=root.getElementsByTagName("student");Student student=null;
// 开始对所获取到的子节点集合对象进行遍历for(int i=0;i<nodeList.getLength();i++){student=new Student();
// 开始获取节点集合当中的第i个结点对象(student结点对象)Element studentNode=(Element) nodeList.item(i);
// System.out.println(studentNode.getTagName());
// 获取当前student结点对象当中的属性值stunoString stuno=studentNode.getAttribute("stuno");
// 获取student结点对象当中的name子节点对象,name结点对象当中的内容是以数组的形式串值过来的Element nameNode=(Element)studentNode.getElementsByTagName("name").item(0);
// 获取name结点对象当中的内容值String name=nameNode.getTextContent();System.out.println("name="+name);
// 获取student结点对象当中的name子节点对象Element ageNode=(Element) studentNode.getElementsByTagName("age").item(0);
// 获取name结点对象当中的内容值String age=ageNode.getTextContent();System.out.println("age="+age);
// 获取student结点对象当中的name子节点对象Element addressNode=(Element) studentNode.getElementsByTagName("address").item(0);
// 获取name结点对象当中的内容值String address=addressNode.getTextContent();student.setStuno(stuno);student.setName(name);student.setAge(age);student.setAddress(address);students.add(student);}for (Student student2 : students) {System.out.println(student2);}}
}