<!-- -------------------------------------------XML文件-------------------------------------------------- -->
<?xml version="1.0" encoding="UTF-8"?>
<Students>
<Student id="11">
<name>吴松</name>
<score>480</score>
</Student>
<Student id="12">
<name>鲁智深</name>
<score>400</score>
</Student>
<Student id="13">
<id>333</id>
<name>盘今年</name>
<score>600</score>
</Student>
</Students>
/* ***********************************************Java Bean********************************************** */
package com.zhouguihua.bean;
public class StudentBean
{
private int id;
private String name;
private float score;
public StudentBean()
{
}
public StudentBean(int id, String name, float score)
{
this.id = id;
this.name = name;
this.score = score;
}
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 float getScore()
{
return score;
}
public void setScore(float score)
{
this.score = score;
}
}
/* ***********************************************解析工厂********************************************** */
package com.zhouguihua.sax;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.zhouguihua.bean.StudentBean;
public class StudentHandler_in extends DefaultHandler
{
//定义集合
private List<StudentBean> list = null;
//定义变量
private String value = null;
//创建类对象
private StudentBean bean = null;
//空参构造
public StudentHandler_in()
{
//初始化集合
this.list = new ArrayList<StudentBean>();
}
//集合的访问器
public List<StudentBean> getList()
{
return list;
}
/**
* 遇到根目录开始节点时触发s
*/
@Override
public void startDocument() throws SAXException
{
System.out.println("开始解析!");
}
/**
* 遇到元素节点时触发
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
if("Student".equals(qName))
{
//初始化bean
this.bean = new StudentBean();
//取出属性
//this.bean.setId(Integer.parseInt(attributes.getValue("学号")));
//this.bean.setName(attributes.getValue("姓名"));
//this.bean.setScore(Float.parseFloat(attributes.getValue("成绩")));
this.bean.setId(Integer.parseInt(attributes.getValue(0)));
}
}
/**
* 扫描到文本时触发
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
//扫描到属性保存到成员Value中
this.value = new String(ch,start,length);
}
/**
* 遇到元素节点结束时触发
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
if("name".equals(qName))
{
this.bean.setName(this.value);
}
else if("score".equals(qName))
{
this.bean.setScore(Float.parseFloat(this.value));
}
else if("Student".equals(qName))
{
//将对象添加到集合
this.list.add(bean);
}
}
/**
* 遇到根目录节点结束时触发
*/
@Override
public void endDocument() throws SAXException
{
System.out.println("结束解析!");
}
}
/* ***********************************************测试类********************************************** */
package com.zhouguihua.test;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import com.zhouguihua.bean.StudentBean;
import com.zhouguihua.sax.StudentHandler_in;
public class StudentTest_in
{
/**
* @param args
*/
public static void main(String[] args)
{
//路径
String uri = "src/sutdent_in.xml";
//获取解析助手
StudentHandler_in handler = new StudentHandler_in();
//创建解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
try
{
//获取解析器
SAXParser work = factory.newSAXParser();
//调用方法
work.parse(uri, handler);
//创建集合,接收
List<StudentBean> list = handler.getList();
if(list.size() > 0)
{
for(StudentBean bean:list)
{
System.out.println("学号为:"+bean.getId());
System.out.println("姓名为:"+bean.getName());
System.out.println("成绩为:"+bean.getScore());
System.out.println("--------------------------------------");
}
}
}
catch(Exception e)
{
System.out.println("error"+e.getMessage());
}
}
}