DOM类:DOMTest.java
import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /* * 读xml文件中的<student>变成一个个Java中Student对象 * 并放入集合中 * */ public class DOMTest { public static void main(String[] args) { List<Student> studentList = null; //1.创建DOM解析器工厂 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //2.设置解析器工厂 factory.setValidating(true); factory.setNamespaceAware(true); try { //3.创建文档构建器 DocumentBuilder builder = factory.newDocumentBuilder(); //4.创建文档对象 //读操作 Document doc = builder.parse("dtd/student.xml"); studentList = getStudents(doc); for(Student stu : studentList){ System.out.println(stu); } } catch (ParserConfigurationException | SAXException | IOException e) { e.printStackTrace(); } } private static List<Student> getStudents(Document doc) { List<Student> studentList = new ArrayList<>(); //拿到根节点/元素 Element studentsElement= doc.getDocumentElement(); //拿到根节点下指定元素,可以有多个,返回值为NodeList(结点的集合) //找到students下所有student结点 NodeList studentNL = studentsElement.getElementsByTagName("student"); Element studentElement = null; Student student = null; for(int i = 0;i < studentNL.getLength();i++){ //获得NodeList中的一个元素,返回值为Node(结点) //相当于拿到了一个<student></student>中的全部信息 //把xml中所有东西都看成结点,包括元素和文本 //一个student元素封装了一个学生的信息 studentElement = (Element)studentNL.item(i); student = getStudent(studentElement); studentList.add(student); } return studentList; } private static Student getStudent(Element studentElement) { Student student = new Student(); //从xml文件中读 //元素的属性值、 //还有 子元素的结点(文本结点)的内容 student.setSid(studentElement.getAttribute("sid")); String firstName; //first_name只有一个,不用for循环遍历了 /* * 公共部分,提取出来做成函数getValue( , ) Element firstNameElement = (Element)studentElement.getElementsByTagName("first_name").item(0); firstName = firstNameElement.getFirstChild().getNodeValue(); System.out.println(firstName); student.setFirstName(firstName); */ student.setFirstName(getValue(studentElement,"first_name")); student.setLastName(getValue(studentElement,"last_name")); student.setAge(Integer.parseInt(getValue(studentElement,"age"))); student.setGender(getValue(studentElement,"gender")); student.setAddress(getAddress(studentElement)); return student; } private static Address getAddress(Element studentElement) { Address address = new Address(); //一个学生只有一个地址 Element addressElement = (Element)studentElement.getElementsByTagName("address").item(0); address.setCity(getValue(addressElement,"city")); address.setStreet(getValue(addressElement,"street")); address.setZip(getValue(addressElement,"zip")); return address; } //元素的文本内容唯一,内容也是结点,叫文本结点 private static String getValue(Element element,String tagName){ Element tagNameElement = (Element)element.getElementsByTagName(tagName).item(0); String value = tagNameElement.getFirstChild().getNodeValue(); return value; } }student.xml
<?xml version="1.0" encoding="UTF-8"?> <students> <student sid="101"> <first_name>Zhang</first_name> <last_name>San</last_name> <age>20</age> <gender>Male</gender> <address> <city>SuZhou</city> <street>XueYuan Rd.</street> <zip>453000</zip> </address> </student> <student sid="102"> <first_name>Li</first_name> <last_name>si</last_name> <age>20</age> <gender>Male</gender> <address> <city>ShangHai</city> <street>NanJing Rd.</street> <zip>200000</zip> </address> </student> </students>Student类
//pojo类 public class Student { private String sid; private String firstName; private String lastName; private int age; private String gender; private Address address; public Student(){} public Student(String sid, String firstName, String lastName, int age, String gender, Address address) { this.sid = sid; this.firstName = firstName; this.lastName = lastName; this.age = age; this.gender = gender; this.address = address; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Student [sid=" + sid + ", firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + ", gender=" + gender + ", address=" + address + "]"; } }Address类
//pojo类 public class Address { private String city; private String street; private String zip; public Address() {} public Address(String city, String street, String zip) { this.city = city; this.street = street; this.zip = zip; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; } @Override public String toString() { return "Address [city=" + city + ", street=" + street + ", zip=" + zip + "]"; } }