sax
DefaultHandler 实现了 Handler接口,使用 sax解析时,只需要继承下
package com.hujl.parserxml.service;
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.hujl.parserxml.entity.Person;
public class SAXPersonHandler extends DefaultHandler {
private List<Person> persons;
private String nodeName=null;
private Person person;
public List<Person> getPersons(){
return persons;
}
@Override
public void startDocument() throws SAXException {
persons=new ArrayList<Person>();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if("person".equals(localName)){
person=new Person();
person.setId(Integer.valueOf(attributes.getValue(0)));
}
nodeName=localName;
}
&#