日期:2014-05-20 浏览次数:20883 次
class SAXHandle extends DefaultHandler {
private List<Employee> list;
private String tagName;
Employee emp = null;
public SAXHandle(List<Employee> list) {
this.list = list;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equals("emp")) {
emp = new Employee();
emp.setId(attributes.getValue("id"));
list.add(emp);
tagName = qName;
} else if (qName.equals("name") || qName.equals("age")
|| qName.equals("dept") || qName.equals("intro")
|| qName.equals("fav")) {
tagName = qName;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
tagName = null;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String value = new String(ch, start, length);
if ("name".equals(tagName)) {
emp.setName(value);
} else if ("age".equals(tagName)) {
emp.setAge(value);
} else if ("dept".equals(tagName)) {
emp.setDept(value);
} else if ("intro".equals(tagName)) {
emp.setIntro(value);
} else if ("fav".equals(tagName)) {
emp.setFav(value);
}
// if (tagName.equals("name")) {
// emp.setName(value);
// System.out.println("value = " + value);
// } else if (tagName.equals("age")) {
// emp.setAge(value);
// System.out.println("value = " + value);
// } else if (tagName.equals("dept")) {
// emp.setDept(value);
// System.out.println("value = " + value);
// } else if (tagName.equals("intro")) {
// emp.setIntro(value);
// System.out.println("value = " + value);
// } else if (tagName.equals("fav")) {
// emp.setFav(value);
// System.out.println("value = " + value);
// }
}
}