xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- peoples相当于表 -->
<!-- 在xml所有标签都称为节点(Node)
元素节点:完成的一个标签(包含属性和文字),最外层包含所有节点的元素节点成为根节点
属性节点:标签的属性
文本节点:文字内容,sax解析把空格和回车当作文本节点
-->
<peoples>
<!-- 把内容当作属性或子标签进行设置都可以,每一个people相当于表中一行数据 -->
<people id="123">
<name>张三</name>
<age>12</age>
</people>
<people id="234">
<name>李四</name>
<age>13</age>
</people>
</peoples>
实体类
public class People {
private Integer id;
private String name;
private Integer age;
public People() {
super();
}
public People(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
解析类
/**
* SAX解析称所有标签为Element
* SAX解析称所有属性节点为Attribute
* XML解析类
* @author jiamingge
*
*/
public class Demo extends DefaultHandler{
//当前节点是谁
private String currNode;
//返回谁,在就上面声明谁
private List<People> list;
private People peo;
public List<People> getList() {
return list;
}
public void setList(List<People> list) {
this.list = list;
}
/**
* 文档开始解析时触发
*/
@Override
public void startDocument() throws SAXException {
super.startDocument();
System.out.println("startDocument");
list = new ArrayList<People>();
}
/**
* 文档解析结束后触发
*/
@Override
public void endDocument() throws SAXException {
super.endDocument();
System.out.println("endDocument");
}
/**
* 开始解析标签时触发
*/
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
System.out.println("startElement");
currNode = qName;
if(currNode.equals("people")){
peo = new People();
peo.setId(Integer.parseInt(attributes.getValue("id")));
}
}
/**
* 解析标签结束后触发
*/
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
System.out.println("endElement");
if("people".equals(qName)){
list.add(peo);
}
}
/**
* 解析当文本节点时触发。回车也认为是一个文本节点
*/
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
//把所有文本节点转换为字符串,如果是空格或回车字符串为"",如果为内容不为""
String content = new String(ch,start,length).trim();
if(!"".equals(content)){
if("name".equals(currNode)){
System.out.println("name"+content+" "+start+" "+length);
peo.setName(content);
}
if("age".equals(currNode)){
System.out.println("age"+content+" "+start+" "+length);
peo.setAge(Integer.parseInt(content));
}
}
}
}
测试类
public class Test {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//1.实例化解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
//2.实例化解析器
SAXParser parser = factory.newSAXParser();
//3.开始解析文件
Demo demo = new Demo();
parser.parse(new File("src/peoples.xml"), demo);//当使用对象demo解析完对应的xml文件后,demo对象中的list就会被填充
for (People peo : demo.getList()) {
System.out.println(peo.toString());
}
}
}