http://blog.youkuaiyun.com/qq_33599978/article/details/68938567
public class Inproe {
public static void main(String[] args) {
SAXParserFactory sFactory = SAXParserFactory.newInstance();
try {
SAXParser parser = sFactory.newSAXParser();
File file = new File("src/com/xm/inproe.xml");
MyHandy mh = new MyHandy();
parser.parse(file, mh);
List<Employee> list = mh.getList();
// System.out.println(list);
for (Employee employee : list) {
System.out.println(employee);
}
} catch (ParserConfigurationException | SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MyHandy extends DefaultHandler{
private List<Employee> list;
private String tagName; //记录标签的值 如sy <sy> </sy>
private Employee employee;
public List<Employee> getList() {
return list;
}
public void setList(List<Employee> list) {
this.list = list;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if ("employee".equals(qName)) {
list.add(employee);
//System.out.println(list);
} else {
//记录标签的值 如sy <sy> </sy>
//防止每个结束标签的时候将换行 赋值给tagName
tagName=null;
}
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if ("employee".equals(qName)) {
employee= new Employee();
employee.setId(Integer.parseInt(attributes.getValue("id")));
employee.setDepName(attributes.getValue("depName"));
}else {
tagName=qName;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String str = new String(ch, start, length);
if ("name".equals(tagName)) {
employee.setName(str);
}else if("age".equals(tagName)){
employee.setAge(Integer.parseInt(str));
}else if ("gender".equals(tagName)) {
employee.setGender(str);
}else if ("salary".equals(tagName)) {
employee.setSalary(Integer.parseInt(str));
}
}
@Override
public void startDocument() throws SAXException {
list = new ArrayList<Employee>();
}
}
public class Employee {
private int id;
private String name;
private String gender;
private int age;
private int salary;
private String depName;
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getDepName() {
return depName;
}
public void setDepName(String depName) {
this.depName = depName;
}
public Employee(int id, String name, String gender, int age, int salary, String depName) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.salary = salary;
this.depName = depName;
}
public Employee() {
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + ", salary=" + salary
+ ", depName=" + depName + "]";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee id="1" depName="教学部">
Text XMl sax
<name>tom</name>
<age>18</age>
<gender>male</gender>
<salary>200</salary>
<test>test</test>
</employee>
<employee id="2" depName="教学部">
<name>java</name>
<age>19</age>
<gender>famale</gender>
<salary>2050</salary>
</employee>
<employee id="3" depName="市场部">
<name>store</name>
<age>20</age>
<gender>famale</gender>
<salary>4000</salary>
</employee>
<employee id="4" depName="市场部">
<name>buy</name>
<age>21</age>
<gender>male</gender>
<salary>45500</salary>
</employee>
</employees>