首页 > 代码库 > xml文件的解析过程详解

xml文件的解析过程详解

student.xml 文件展示: <?xml version="1.0" encoding="utf-8" ?><person>   <student id=1>        <name>余超</name>        <sex>男</sex>        <desc>一个执着而又天真的孩子</desc>           </student>     <student id=2>        <name>马靖</name>        <sex>女</sex>        <desc>一个特别难追求的女孩子</desc>           </student></person>Student实体类的展示:其作用就是用于临时保存xml文件中的数据到属性中package net.nyist.xmlparse.domain;import java.io.Serializable;/** * @author yuchao *  * @school 南阳理工软件学院移动设备应用与开发移动四班 *  * @email yu0312chao@163.com *  * @time 2014年9月30日 下午10:52:47 */@SuppressWarnings("serial")public class Student implements Serializable {    private int id;    private String name;    private String sex;    private String desc;    public Student() {    }    public Student(int id, String name, String sex, String desc) {        this.id = id;        this.name = name;        this.sex = sex;        this.desc = desc;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public String getDesc() {        return desc;    }    public void setDesc(String desc) {        this.desc = desc;    }    @Override    public String toString() {        return "Student [id=" + id + ", name=" + name + ", sex=" + sex                + ", desc=" + desc + "]";    }}

方法一:通过DOM来解析XML文件

package net.nyist.xmlparse.parse.dom;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import net.nyist.xmlparse.domain.Student;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;/** * @author yuchao *  * @school 南阳理工软件学院移动设备应用与开发移动四班 *  * @email yu0312chao@163.com *  * @time 2014年9月30日 下午11:12:57 */public class DocumentBuilderFactoryDemo {    public static void main(String[] args) {        /** 首先得到:得到 DOM 解析器的工厂实例 */        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory                .newInstance();        List<Student> students = new ArrayList<Student>();        try {            /** 然后从 DOM 工厂获得 DOM 解析器 */            DocumentBuilder documentBuilder = documentBuilderFactory                    .newDocumentBuilder();            InputStream is = DocumentBuilderFactoryDemo.class.getClassLoader()                    .getResourceAsStream("student.xml");            Document document = documentBuilder.parse(is);            /** 得到 XML 文档的根节点 */            Element element = document.getDocumentElement();            NodeList nodeList = element.getElementsByTagName("student");            Student student = null;            for (int i = 0; i < nodeList.getLength(); i++) {                Node node = nodeList.item(i);                int id = Integer.parseInt(node.getAttributes()                        .getNamedItem("id").getNodeValue());                String nameValue = "";                String sexValue = "";                String descValue = "";                if (node.hasChildNodes()) {                    NodeList childNodes = node.getChildNodes();                    for (int j = 0; j < childNodes.getLength(); j++) {                        Node node2 = childNodes.item(j);                        if ("name".equals(node2.getNodeName())) {                            nameValue = node2.getFirstChild().getTextContent();                        } else if ("sex".equals(node2.getNodeName())) {                            sexValue = node2.getFirstChild().getTextContent();                        } else if ("desc".equals(node2.getNodeName())) {                            descValue = node2.getFirstChild().getTextContent();                        }                    }                    student = new Student(id, nameValue, sexValue, descValue);                    students.add(student);                }            }        } catch (Exception e) {            e.printStackTrace();        }        for (int i = 0; students != null && students.size() > 0                && i < students.size(); i++) {            System.out.println(students.get(i));        }    }}

xml文件的解析过程详解