解析结果:
运行时间:109 毫秒
班级:30713
学号:3071300
姓名:小明
性别:男
年龄:10
班级:30713
学号:3071301
姓名:小花
性别:女
年龄:20
班级:30713
学号:3071302
姓名:不知道
性别:男
年龄:15
班级:30714
学号:3071400
姓名:小明
性别:男
年龄:10
班级:30714
学号:3071401
姓名:小花
性别:女
年龄:20
班级:30714
学号:3071402
姓名:不知道
性别:男
年龄:15
数据模型:
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;
}
}
需要解析的文件
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>
解析程序
ParseXml.java
package com.xie.xmlparse.jdom;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.xie.xmlparse.dom4j.modal.Student;
//将xml中的数据读出
public class ParseXml {
List<Student> aList=null;
private List<Student> parseWithJdom(){
long lasting = System.currentTimeMillis();
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File("D://project//XmlParse//xmlFiles//student.xml"));
Element root =doc.getRootElement();
List childClass=root.getChildren();
aList=new ArrayList<Student>();
for (int i = 0; i < childClass.size(); i++ ) {
Element ele=(Element) childClass.get(i);
List childStu=ele.getChildren();
for(int j=0;j<childStu.size();j++){
Student s=new Student();
Element ele1=(Element) childStu.get(j);
/* System.out.println(ele.getAttributeValue("name"));
System.out.println(ele1.getChildText("stuid"));
System.out.println(ele1.getChildText("stuname"));
System.out.println(ele1.getChildText("stusex"));
System.out.println(ele1.getChildText("stuage"));*/
s.setClassId(Long.parseLong(ele.getAttributeValue("name")));
s.setStuId(Long.parseLong(ele1.getChildText("stuid")));
s.setStuName(ele1.getChildText("stuname"));
s.setStuSex(ele1.getChildText("stusex"));
s.setStuAge(Integer.parseInt(ele1.getChildText("stuage")));
aList.add(s);
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("运行时间:"+(System.currentTimeMillis() - lasting)+" 毫秒");
return aList;
}
public static void main(String[] args){
List<Student> asList=new ParseXml().parseWithJdom();
for (Iterator<Student> iterator = asList.iterator(); iterator.hasNext();) {
Student s = (Student) iterator.next();
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());
}
}
}