Dom解析器使用实例

来源:互联网 发布:mac模拟器电脑版 编辑:程序博客网 时间:2024/05/17 03:23

dom解析器

dom全部文件读入到内存中,然后使用dom的api遍历所有数据,检索想要的数据,这种方式显然是一种比较消耗内存的方式,对于像手机这样的移动设备来讲,内存是非常有限的,所以对于比较大的XML文件,不推荐使用这种方式,但是Dom也有它的优点,它比较直观,在一些方面比SAX方式比较简单。在xml文档比较小的情况下也可以考虑使用dom方式


解析步骤:

1、创建并获取DOM解析器工厂对象

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

2、创建并获取DOM解析器对象

DocumentBuilder builder = factory.newDocumentBuilder();

3、创建并获取Document对象

Document document = builder.parse(inputStream);

4、获得文档的根元素,在下面的xml文件中根元素为courseInfo

Element root = document.getDocumentElement();


5、获取名称为student的子节点(只有一个)

Element studentElement = (Element) root.getElementsByTagName("student").item(0);

6、获取名称为courseList的子节点列表(有多个)

NodeList courseNodeList = root.getElementsByTagName("courseList");

7、遍历节点列表

for (int i = 0; i < nodeList.getLength(); i++) {            //获得一个子节点            Element element = (Element) nodeList.item(i);             String courseName = element.getElementsByTagName("courseName").item(0).getFirstChild().getNodeValue();             String courseId = element.getElementsByTagName("courseId").item(0).getFirstChild().getNodeValue();             String teacherName = element.getElementsByTagName("teacherName").item(0).getFirstChild().getNodeValue();             String teacherId = element.getElementsByTagName("teacherId").item(0).getFirstChild().getNodeValue();             String room = element.getElementsByTagName("room").item(0).getFirstChild().getNodeValue();             int day = Integer.parseInt(element.getElementsByTagName("day").item(0).getFirstChild().getNodeValue());             int startWeek = Integer.parseInt(element.getElementsByTagName("startWeek").item(0).getFirstChild().getNodeValue());             int totalWeeks = Integer.parseInt(element.getElementsByTagName("totalWeeks").item(0).getFirstChild().getNodeValue());             int startSection = Integer.parseInt(element.getElementsByTagName("startSection").item(0).getFirstChild().getNodeValue());             int totalSection = Integer.parseInt(element.getElementsByTagName("totalSection").item(0).getFirstChild().getNodeValue());             int singleOrDouble = Integer.parseInt(element.getElementsByTagName("singleOrDouble").item(0).getFirstChild().getNodeValue());            course = new Course(courseName,courseId,teacherName,teacherId,room,day,startWeek,totalWeeks,startSection,totalSection,singleOrDouble);            courseList.add(course);        }



<?xml version="1.0" encoding="UTF-8" standalone="yes"?><courseInfo>    <courseList>        <courseId>000001</courseId>        <courseName>C语言</courseName>        <day>1</day>        <room>励耘楼</room>        <singleOrDouble>0</singleOrDouble>        <startSection>1</startSection>        <startWeek>1</startWeek>        <teacherId>100001</teacherId>        <teacherName>肖红</teacherName>        <totalSection>2</totalSection>        <totalWeeks>17</totalWeeks>    </courseList>    <courseList>        <courseId>000002</courseId>        <courseName>计算机算法</courseName>        <day>1</day>        <room>综合楼</room>        <singleOrDouble>0</singleOrDouble>        <startSection>5</startSection>        <startWeek>1</startWeek>        <teacherId>100002</teacherId>        <teacherName>陈红</teacherName>        <totalSection>2</totalSection>        <totalWeeks>17</totalWeeks>    </courseList>    <courseList>        <courseId>000003</courseId>        <courseName>C++面向对象编程语言</courseName>        <day>3</day>        <room>综合楼</room>        <singleOrDouble>0</singleOrDouble>        <startSection>5</startSection>        <startWeek>1</startWeek>        <teacherId>100003</teacherId>        <teacherName>杨平</teacherName>        <totalSection>2</totalSection>        <totalWeeks>17</totalWeeks>    </courseList>    <student>        <grade>2013</grade>        <major>计算机科学与技术</major>        <name>张三</name>        <schools>信息技术学院</schools>        <studentid>1301010005</studentid>    </student></courseInfo>

实体类

Student.java

public class Student {//学生姓名private String name;//学号private String studentid;//专业private String major;//学院private String schools;//年级private String grade;public Student(String name, String studentid, String major, String schools, String grade) {super();this.name = name;this.studentid = studentid;this.major = major;this.schools = schools;this.grade = grade;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getStudentid() {return studentid;}public void setStudentid(String studentid) {this.studentid = studentid;}public String getMajor() {return major;}public void setMajor(String major) {this.major = major;}public String getSchools() {return schools;}public void setSchools(String schools) {this.schools = schools;}public String getGrade() {return grade;}public void setGrade(String grade) {this.grade = grade;}}


Course.java

public class Course {//课程名称private String courseName;//课程idprivate String courseId;//教室名称private String teacherName;//教室idprivate String teacherId;//教室private String room;//具体到星期几private int day;//开始周private int startWeek;//总周数private int totalWeeks;//开始上课节数private int startSection;//上课总周数private int totalSection;//单双周,单周为1,双周为2,部分为0private int singleOrDouble;public Course(String courseName, String courseId, String teacherName, String teacherId, String room, int day,int startWeek, int totalWeeks, int startSection, int totalSection, int singleOrDouble) {super();this.courseName = courseName;this.courseId = courseId;this.teacherName = teacherName;this.teacherId = teacherId;this.room = room;this.day = day;this.startWeek = startWeek;this.totalWeeks = totalWeeks;this.startSection = startSection;this.totalSection = totalSection;this.singleOrDouble = singleOrDouble;}public int getSingleOrDouble() {return singleOrDouble;}public void setSingleOrDouble(int singleOrDouble) {this.singleOrDouble = singleOrDouble;}public String getCourseName() {return courseName;}public void setCourseName(String courseName) {this.courseName = courseName;}public String getCourseId() {return courseId;}public void setCourseId(String courseId) {this.courseId = courseId;}public String getTeacherName() {return teacherName;}public void setTeacherName(String teacherName) {this.teacherName = teacherName;}public String getTeacherId() {return teacherId;}public void setTeacherId(String teacherId) {this.teacherId = teacherId;}public String getRoom() {return room;}public void setRoom(String room) {this.room = room;}public int getDay() {return day;}public void setDay(int day) {this.day = day;}public int getStartWeek() {return startWeek;}public void setStartWeek(int startWeek) {this.startWeek = startWeek;}public int getTotalWeeks() {return totalWeeks;}public void setTotalWeeks(int totalWeeks) {this.totalWeeks = totalWeeks;}public int getStartSection() {return startSection;}public void setStartSection(int startSection) {this.startSection = startSection;}public int getTotalSection() {return totalSection;}public void setTotalSection(int totalSection) {this.totalSection = totalSection;}}


CourseInfo.java

import java.util.ArrayList;import java.util.List;public class CourseInfo {//学生private Student student;//课程列表private List<Course> courseList = new ArrayList<Course>();public CourseInfo(){}public CourseInfo(Student student, List<Course> courseList) {super();this.student = student;this.courseList = courseList;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}public List<Course> getCourseList() {return courseList;}public void setCourseList(List<Course> courseList) {this.courseList = courseList;}}

xml.java

package com.example.administrator.httpconnectlogin.Tools;import com.example.administrator.httpconnectlogin.entity.Course;import com.example.administrator.httpconnectlogin.entity.CourseInfo;import com.example.administrator.httpconnectlogin.entity.Student;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;/** * Created by Administrator on 2016/10/12. */public class XmlUtil {    public static CourseInfo xmlToObject_CourseInfo(String xml){        CourseInfo courseInfo = null;        Student student = null;        List<Course> courseList = new ArrayList<>();        InputStream inputStream = new ByteArrayInputStream(xml.getBytes());        //创建DOM解析器工厂        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();        try {            //创建DOM解析器            DocumentBuilder builder = factory.newDocumentBuilder();            //获取到了document对象            //Document document = builder.parse(inputStream);            Document document = builder.parse(inputStream);            //获得文档的根元素            Element root = document.getDocumentElement();            //获得名称为student的元素节点(student只有一个节点)            Element studentElement = (Element) root.getElementsByTagName("student").item(0);            student = getStudentObject(studentElement);            //得到文档中名称为courseList的元素的节点列表            NodeList courseNodeList = root.getElementsByTagName("courseList");            courseList =getCourseListObject(courseNodeList);            courseInfo = new CourseInfo(student,courseList);        } catch (ParserConfigurationException e) {            e.printStackTrace();        } catch (SAXException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return courseInfo;    }    private static Student getStudentObject(Element element){        String name = element.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();        String studentid = element.getElementsByTagName("studentid").item(0).getFirstChild().getNodeValue();        String major = element.getElementsByTagName("major").item(0).getFirstChild().getNodeValue();        String schools =element.getElementsByTagName("schools").item(0).getFirstChild().getNodeValue();        String grade = element.getElementsByTagName("grade").item(0).getFirstChild().getNodeValue();        Student student = new Student(name,studentid,major,schools,grade);        return student;    }    private static List<Course> getCourseListObject(NodeList nodeList){        List<Course> courseList = new ArrayList<>();        Course course = null;        for (int i = 0; i < nodeList.getLength(); i++) {            //获得一个子节点            Element element = (Element) nodeList.item(i);             String courseName = element.getElementsByTagName("courseName").item(0).getFirstChild().getNodeValue();             String courseId = element.getElementsByTagName("courseId").item(0).getFirstChild().getNodeValue();             String teacherName = element.getElementsByTagName("teacherName").item(0).getFirstChild().getNodeValue();             String teacherId = element.getElementsByTagName("teacherId").item(0).getFirstChild().getNodeValue();             String room = element.getElementsByTagName("room").item(0).getFirstChild().getNodeValue();             int day = Integer.parseInt(element.getElementsByTagName("day").item(0).getFirstChild().getNodeValue());             int startWeek = Integer.parseInt(element.getElementsByTagName("startWeek").item(0).getFirstChild().getNodeValue());             int totalWeeks = Integer.parseInt(element.getElementsByTagName("totalWeeks").item(0).getFirstChild().getNodeValue());             int startSection = Integer.parseInt(element.getElementsByTagName("startSection").item(0).getFirstChild().getNodeValue());             int totalSection = Integer.parseInt(element.getElementsByTagName("totalSection").item(0).getFirstChild().getNodeValue());             int singleOrDouble = Integer.parseInt(element.getElementsByTagName("singleOrDouble").item(0).getFirstChild().getNodeValue());            course = new Course(courseName,courseId,teacherName,teacherId,room,day,startWeek,totalWeeks,startSection,totalSection,singleOrDouble);            courseList.add(course);        }        return courseList;    }}



0 0