余超

xml文件的解析过程详解

  • 格式:pdf
  • 大小:96.20 KB
  • 文档页数:6

xml⽂件的解析过程详解

XML项⽬⼯程展⽰图如下图:

student.xml ⽂件展⽰:

余超

⼀个执着⽽⼜天真的孩⼦

马靖

⼀个特别难追求的⼥孩⼦

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) {

long start = System.currentTimeMillis();

/** ⾸先得到:得到 DOM 解析器的⼯⼚实例 */

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory

.newInstance();

List students = new ArrayList();

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));

}

System.out.println("共⽤时:"+(System.currentTimeMillis()-start));

}

}

⽅法⼆:⽤SAX解析XML⽂件

package net.nyist.xmlparse.parse.sax;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import net.nyist.xmlparse.domain.Student;

import org.xml.sax.Attributes;

import org.xml.sax.InputSource;

import org.xml.sax.Locator;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

/**

* @author yuchao

*

* @school 南阳理⼯软件学院移动设备应⽤与开发移动四班

*

* @email yu0312chao@163.com

*

* @time 2014年10⽉1⽇ 下午6:38:52

*

* @deprecated 本类是⽤来对于XML⽂本的解析类

*/

public class PersonHandle extends DefaultHandler {

private Student student;

private List students;

/**对于这个属性的使⽤主要作⽤是为了⽤来存放标签的名称,由此⽽获得这个标签下的⽂本内容的值*/

private String tagName;

public List getStudents() {

return students;

}

public InputSource resolveEntity(String publicId, String systemId)

throws IOException, SAXException {

return null;

}

public void setDocumentLocator(Locator locator) {

System.out.println("加载器开始处理xml⽂档........");

}

public void startDocument() throws SAXException {

System.out.println("⽂档开始加载.........");

students = new ArrayList();

}

public void endDocument() throws SAXException {

System.out.println("xml⽂档处理结束.........");

}

public void startElement(String uri, String localName, String qName,

Attributes attributes) throws SAXException {

tagName = qName;

if ("student".equals(qName)) {

student = new Student();