Java中Sax解析XML
2024-10-20 01:41:53
SAX基于事件的解析,解析器在一次读取XML文件中根据读取的数据产生相应的事件,由应用程序实现相应的事件处理逻辑,即它是一种“推”的解析方式;
这种解析方法速度快、占用内存少,但是它需要应用程序自己处理解析器的状态,实现起来会比较麻烦。
dom4j解析xml: http://www.cnblogs.com/gavinYang/p/3505535.html
jdom解析xml: http://www.cnblogs.com/gavinYang/p/3505530.html
dom解析: http://www.cnblogs.com/gavinYang/p/3505523.html
Java代码:
package com.test; import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler; public class SaxXML { public static void main(String[] args) {
File file = new File("e:/People.xml");
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = spf.newSAXParser();
SaxHandler handler = new SaxHandler("People");
parser.parse(new FileInputStream(file), handler); List<People> peopleList = handler.getPeoples();
for(People people : peopleList){
System.out.println(people.getId()+"\t"+people.getName()+"\t"+people.getEnglishName()+"\t"+people.getAge());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} } class SaxHandler extends DefaultHandler {
private List<People> peoples = null;
private People people;
private String currentTag = null;
private String currentValue = null;
private String nodeName = null; public List<People> getPeoples() {
return peoples;
} public SaxHandler(String nodeName) {
this.nodeName = nodeName;
} @Override
public void startDocument() throws SAXException {
// TODO 当读到一个开始标签的时候,会触发这个方法
super.startDocument(); peoples = new ArrayList<People>();
} @Override
public void endDocument() throws SAXException {
// TODO 自动生成的方法存根
super.endDocument();
} @Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
// TODO 当遇到文档的开头的时候,调用这个方法
super.startElement(uri, localName, name, attributes); if (name.equals(nodeName)) {
people = new People();
}
if (attributes != null && people != null) {
for (int i = 0; i < attributes.getLength(); i++) {
if(attributes.getQName(i).equals("id")){
people.setId(attributes.getValue(i));
}
else if(attributes.getQName(i).equals("en")){
people.setEnglishName(attributes.getValue(i));
}
}
}
currentTag = name;
} @Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO 这个方法用来处理在XML文件中读到的内容
super.characters(ch, start, length); if (currentTag != null && people != null) {
currentValue = new String(ch, start, length);
if (currentValue != null && !currentValue.trim().equals("") && !currentValue.trim().equals("\n")) {
if(currentTag.equals("Name")){
people.setName(currentValue);
}
else if(currentTag.equals("Age")){
people.setAge(currentValue);
}
}
}
currentTag = null;
currentValue = null;
} @Override
public void endElement(String uri, String localName, String name)
throws SAXException {
// TODO 在遇到结束标签的时候,调用这个方法
super.endElement(uri, localName, name); if (name.equals(nodeName)) {
peoples.add(people);
}
} }
People对象:
package com.test; public class People {
private String id;
private String name;
private String englishName;
private String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEnglishName() {
return englishName;
}
public void setEnglishName(String englishName) {
this.englishName = englishName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
} }
xml:
<?xml version="1.0" encoding="UTF-8"?>
<PeopleList>
<People id="1">
<Name en='zhangsan'>张三</Name>
<Age>20</Age>
</People>
<People id="2">
<Name en='lisi'>李四</Name>
<Age>30</Age>
</People>
</PeopleList>
原文出自:http://blog.csdn.net/zyting_love/article/details/6929307
最新文章
- 【项目】百度搜索广告CTR预估
- selenium-打开IE浏览器遇到问题记录
- Windows Phone 8.0 SDK Update(10322) Released
- [转]Cookie/Session机制详解
- 1029-c语言文法的理解
- 51nod-1537 1537 分解(矩阵快速幂+找规律)
- Introduction to Machine Learning
- Android Handler传递参数动态更新UI界面demo
- Java连接redis的使用演示样例
- Lambda表达式、依赖倒置
- 二十二、oracle pl/sql分类二 函数
- vue router-link 上添加点击事件
- HTML5 进阶系列:拖放 API 实现拖放排序(转载)
- awk删除重复文件
- js vue 请求
- linux软件管理 YUM命令
- MySQL 联合索引测试3
- hdu 1392 Surround the Trees 凸包模板
- Java开发工程师(Web方向) - 01.Java Web开发入门 - 第5章.Git
- entering power save mode无法开机解决办法
热门文章
- MD5进行加密操作
- IDEA Failed to prepare an update: Temp directory inside installation
- 洛谷P5020货币系统
- 「CodeForces - 50C 」Happy Farm 5 (几何)
- 【题解】 bzoj3555: [Ctsc2014]企鹅QQ (字符串Hash)
- PKUWC2019 凉凉记
- The Python Challenge 谜题全解(持续更新)
- 【BZOJ5335】[TJOI2018]智力竞赛(二分图匹配)
- 自适应PC端网页制作使用REM
- 【Linux】linux正则表达式及通配符