轻松使用sax解析xml
sax方式解析xml的优点:
只要有jdk的存在,严格意义上来说是jre,它就可以运行,不依赖任何第三方的jar包。在移动设备上尤为广泛使用。比如Android就使用它来解析xml。当然Android更为推荐的是采用xmlpull来解析xml。这种方式也同样适合javase。我在后期会为大家准备使用xmlpull解析xml的demo。敬请关注。
xml文件:
注:如需运行代码,添加Student.xml到你的src目录下即可
点我下载demo
只要有jdk的存在,严格意义上来说是jre,它就可以运行,不依赖任何第三方的jar包。在移动设备上尤为广泛使用。比如Android就使用它来解析xml。当然Android更为推荐的是采用xmlpull来解析xml。这种方式也同样适合javase。我在后期会为大家准备使用xmlpull解析xml的demo。敬请关注。
xml文件:
<?
xml version="1.0" encoding="UTF-8"
?>
< root >
< student id ="1" group ="1" >
< name > 张三 </ name >
< sex > 男 </ sex >
< age > 18 </ age >
< email > [email protected] </ email >
< birthday > 1987-06-08 </ birthday >
< memo > 好学生 </ memo >
</ student >
< student id ="2" group ="2" >
< name > 李四 </ name >
< sex > 女 </ sex >
< age > 18 </ age >
< email > [email protected] </ email >
< birthday > 1987-06-08 </ birthday >
< memo > 好学生 </ memo >
</ student >
< student id ="3" group ="3" >
< name > 小王 </ name >
< sex > 男 </ sex >
< age > 18 </ age >
< email > [email protected] </ email >
< birthday > 1987-06-08 </ birthday >
< memo > 好学生 </ memo >
</ student >
< student id ="4" group ="4" >
< name > 小张 </ name >
< sex > 男 </ sex >
< age > 18 </ age >
< email > [email protected] </ email >
< birthday > 1987-06-08 </ birthday >
< memo > 好学生 </ memo >
</ student >
< student id ="5" group ="5" >
< name > 小明 </ name >
< sex > 男 </ sex >
< age > 18 </ age >
< email > [email protected] </ email >
< birthday > 1987-06-08 </ birthday >
< memo > 好学生 </ memo >
</ student >
</ root >
xml对应的Javabean:
< root >
< student id ="1" group ="1" >
< name > 张三 </ name >
< sex > 男 </ sex >
< age > 18 </ age >
< email > [email protected] </ email >
< birthday > 1987-06-08 </ birthday >
< memo > 好学生 </ memo >
</ student >
< student id ="2" group ="2" >
< name > 李四 </ name >
< sex > 女 </ sex >
< age > 18 </ age >
< email > [email protected] </ email >
< birthday > 1987-06-08 </ birthday >
< memo > 好学生 </ memo >
</ student >
< student id ="3" group ="3" >
< name > 小王 </ name >
< sex > 男 </ sex >
< age > 18 </ age >
< email > [email protected] </ email >
< birthday > 1987-06-08 </ birthday >
< memo > 好学生 </ memo >
</ student >
< student id ="4" group ="4" >
< name > 小张 </ name >
< sex > 男 </ sex >
< age > 18 </ age >
< email > [email protected] </ email >
< birthday > 1987-06-08 </ birthday >
< memo > 好学生 </ memo >
</ student >
< student id ="5" group ="5" >
< name > 小明 </ name >
< sex > 男 </ sex >
< age > 18 </ age >
< email > [email protected] </ email >
< birthday > 1987-06-08 </ birthday >
< memo > 好学生 </ memo >
</ student >
</ root >
package
com.parsexml.entity;
public class Student {
private int id;
private int group;
private String name;
private String sex;
private int age;
private String email;
private String memo;
private String birthday;
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
public int getGroup() {
return group;
}
public void setGroup( int group) {
this .group = group;
}
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 int getAge() {
return age;
}
public void setAge( int age) {
this .age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this .email = email;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this .memo = memo;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this .birthday = birthday;
}
}
开始解析:
public class Student {
private int id;
private int group;
private String name;
private String sex;
private int age;
private String email;
private String memo;
private String birthday;
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
public int getGroup() {
return group;
}
public void setGroup( int group) {
this .group = group;
}
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 int getAge() {
return age;
}
public void setAge( int age) {
this .age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this .email = email;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this .memo = memo;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this .birthday = birthday;
}
}
package
com.parsexml;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
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;
import com.parsexml.entity.Student;
/**
* 功能描述:采用sax方式解析XML<br>
*
* @author sxyx2008
*
*/
public class SaxParseXml extends DefaultHandler{
// 存放遍历集合
private List < Student > list;
// 构建Student对象
private Student student;
// 用来存放每次遍历后的元素名称(节点名称)
private String tagName;
public List < Student > getList() {
return list;
}
public void setList(List < Student > list) {
this .list = list;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this .student = student;
}
public String getTagName() {
return tagName;
}
public void setTagName(String tagName) {
this .tagName = tagName;
}
// 只调用一次 初始化list集合
@Override
public void startDocument() throws SAXException {
list = new ArrayList < Student > ();
}
// 调用多次 开始解析
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equals( " student " )){
student = new Student();
// 获取student节点上的id属性值
student.setId(Integer.parseInt(attributes.getValue( 0 )));
// 获取student节点上的group属性值
student.setGroup(Integer.parseInt(attributes.getValue( 1 )));
}
this .tagName = qName;
}
// 调用多次
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equals( " student " )){
this .list.add( this .student);
}
this .tagName = null ;
}
// 只调用一次
@Override
public void endDocument() throws SAXException {
}
// 调用多次
@Override
public void characters( char [] ch, int start, int length)
throws SAXException {
if ( this .tagName != null ){
String date = new String(ch,start,length);
if ( this .tagName.equals( " name " )){
this .student.setName(date);
}
else if ( this .tagName.equals( " sex " )){
this .student.setSex(date);
}
else if ( this .tagName.equals( " age " )){
this .student.setAge(Integer.parseInt(date));
}
else if ( this .tagName.equals( " email " )){
this .student.setEmail(date);
}
else if ( this .tagName.equals( " birthday " )){
this .student.setBirthday(date);
}
else if ( this .tagName.equals( " memo " )){
this .student.setMemo(date);
}
}
}
public static void main(String[] args) {
SAXParser parser = null ;
try {
// 构建SAXParser
parser = SAXParserFactory.newInstance().newSAXParser();
// 实例化 DefaultHandler对象
SaxParseXml parseXml = new SaxParseXml();
// 加载资源文件 转化为一个输入流
InputStream stream = SaxParseXml. class .getClassLoader().getResourceAsStream( " Student.xml " );
// 调用parse()方法
parser.parse(stream, parseXml);
// 遍历结果
List < Student > list = parseXml.getList();
for (Student student:list){
System.out.println( " id: " + student.getId() + " \tgroup: " + student.getGroup() + " \tname: " + student.getName() + " \tsex: " + student.getSex() + " \tage: " + student.getAge() + " \temail: " + student.getEmail() + " \tbirthday: " + student.getBirthday() + " \tmemo: " + student.getMemo());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
效果图:
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
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;
import com.parsexml.entity.Student;
/**
* 功能描述:采用sax方式解析XML<br>
*
* @author sxyx2008
*
*/
public class SaxParseXml extends DefaultHandler{
// 存放遍历集合
private List < Student > list;
// 构建Student对象
private Student student;
// 用来存放每次遍历后的元素名称(节点名称)
private String tagName;
public List < Student > getList() {
return list;
}
public void setList(List < Student > list) {
this .list = list;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this .student = student;
}
public String getTagName() {
return tagName;
}
public void setTagName(String tagName) {
this .tagName = tagName;
}
// 只调用一次 初始化list集合
@Override
public void startDocument() throws SAXException {
list = new ArrayList < Student > ();
}
// 调用多次 开始解析
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equals( " student " )){
student = new Student();
// 获取student节点上的id属性值
student.setId(Integer.parseInt(attributes.getValue( 0 )));
// 获取student节点上的group属性值
student.setGroup(Integer.parseInt(attributes.getValue( 1 )));
}
this .tagName = qName;
}
// 调用多次
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equals( " student " )){
this .list.add( this .student);
}
this .tagName = null ;
}
// 只调用一次
@Override
public void endDocument() throws SAXException {
}
// 调用多次
@Override
public void characters( char [] ch, int start, int length)
throws SAXException {
if ( this .tagName != null ){
String date = new String(ch,start,length);
if ( this .tagName.equals( " name " )){
this .student.setName(date);
}
else if ( this .tagName.equals( " sex " )){
this .student.setSex(date);
}
else if ( this .tagName.equals( " age " )){
this .student.setAge(Integer.parseInt(date));
}
else if ( this .tagName.equals( " email " )){
this .student.setEmail(date);
}
else if ( this .tagName.equals( " birthday " )){
this .student.setBirthday(date);
}
else if ( this .tagName.equals( " memo " )){
this .student.setMemo(date);
}
}
}
public static void main(String[] args) {
SAXParser parser = null ;
try {
// 构建SAXParser
parser = SAXParserFactory.newInstance().newSAXParser();
// 实例化 DefaultHandler对象
SaxParseXml parseXml = new SaxParseXml();
// 加载资源文件 转化为一个输入流
InputStream stream = SaxParseXml. class .getClassLoader().getResourceAsStream( " Student.xml " );
// 调用parse()方法
parser.parse(stream, parseXml);
// 遍历结果
List < Student > list = parseXml.getList();
for (Student student:list){
System.out.println( " id: " + student.getId() + " \tgroup: " + student.getGroup() + " \tname: " + student.getName() + " \tsex: " + student.getSex() + " \tage: " + student.getAge() + " \temail: " + student.getEmail() + " \tbirthday: " + student.getBirthday() + " \tmemo: " + student.getMemo());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注:如需运行代码,添加Student.xml到你的src目录下即可
点我下载demo