数据模型:
Student.java
package com.xie.xmlparse.dom4j.modal;
public class Student {
private Long classId;
private Long stuId;
private String stuName;
private String stuSex;
private int stuAge;
public Long getClassId() {
return classId;
}
public void setClassId(Long classId) {
this.classId = classId;
}
public Long getStuId() {
return stuId;
}
public void setStuId(Long stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
}
需要解析的xml
//student.xml
<?xml version="1.0" encoding="UTF-8"?>
<school>
<class name="030713">
<student>
<stuid>03071300</stuid>
<stuname>小明</stuname>
<stusex>男</stusex>
<stuage>10</stuage>
</student>
<student>
<stuid>03071301</stuid>
<stuname>小花</stuname>
<stusex>女</stusex>
<stuage>20</stuage>
</student>
<student>
<stuid>03071302</stuid>
<stuname>不知道</stuname>
<stusex>男</stusex>
<stuage>15</stuage>
</student>
</class>
<class name="030714">
<student>
<stuid>03071400</stuid>
<stuname>小明</stuname>
<stusex>男</stusex>
<stuage>10</stuage>
</student>
<student>
<stuid>03071401</stuid>
<stuname>小花</stuname>
<stusex>女</stusex>
<stuage>20</stuage>
</student>
<student>
<stuid>03071402</stuid>
<stuname>不知道</stuname>
<stusex>男</stusex>
<stuage>15</stuage>
</student>
</class>
</school>
//解析xml的java代码
//XmlParse.java
package com.xie.xmlparse.dom;
import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.xie.xmlparse.dom4j.modal.Student;
//将xml中的数据读出
public class ParseXml {
public ArrayList<Student> domParseXml(){
long lasting = System.currentTimeMillis();
ArrayList<Student> aslist=new ArrayList<Student>();
try {
File f=new File("D://project//XmlParse//xmlFiles//student.xml");
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(f);
NodeList nodes=doc.getElementsByTagName("class");
for (int i = 0; i < nodes.getLength(); i++) {
if (nodes.item(i).getNodeType()==Node.ELEMENT_NODE) {
Element ele=(Element)nodes.item(i);
NodeList nodes1=ele.getElementsByTagName("student");
for (int j = 0; j < nodes1.getLength(); j++) {
if (nodes1.item(j).getNodeType()==Node.ELEMENT_NODE) {
Student s=new Student();
Element ele1=(Element)nodes1.item(j);
s.setClassId(Long.parseLong(ele.getAttribute("name")));
s.setStuId(Long.parseLong(ele1.getElementsByTagName("stuid").item(0).getFirstChild().getNodeValue()));
s.setStuName(ele1.getElementsByTagName("stuname").item(0).getFirstChild().getNodeValue());
s.setStuSex(ele1.getElementsByTagName("stusex").item(0).getFirstChild().getNodeValue());
s.setStuAge(Integer.parseInt(ele1.getElementsByTagName("stuage").item(0).getFirstChild().getNodeValue()));
aslist.add(s);
/* System.out.println(ele.getAttribute("name"));
System.out.println(ele1.getElementsByTagName("stuid").item(0).getFirstChild().getNodeValue());
System.out.println(ele1.getElementsByTagName("stuname").item(0).getFirstChild().getNodeValue());
System.out.println(ele1.getElementsByTagName("stusex").item(0).getFirstChild().getNodeValue());
System.out.println(ele1.getElementsByTagName("stuage").item(0).getFirstChild().getNodeValue());*/
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("运行时间:"+(System.currentTimeMillis() - lasting)+" 毫秒");
return aslist;
}
public static void main(String[] args){
ArrayList< Student> a=new ParseXml().domParseXml();
for (int i = 0; i < a.size(); i++) {
Student s=a.get(i);
System.out.println(s.getClassId());
System.out.println(s.getStuId());
System.out.println(s.getStuName());
System.out.println(s.getStuSex());
System.out.println(s.getStuAge());
}
}
}