xml解析--SAX解析
xml解析---SAX
工程结构图:
[img]
[/img]
这里的stu.xml文件是在src下的,而不是包里面的!
一、xml文件(随便写了一个)
<?xml version="1.0" encoding="utf-8"?><students><student id="1"><name>tom</name><age>20</age></student><student id="2"><name>kite</name><age>21</age></student><student id="3"><name>rose</name><age>22</age></student></students>
package com.amaker.sax;public class Student {private int id;private int age;private String name;/** * 无参构造方法 */public Student() {super();}/** * 带参构造方法 */public Student(int id, int age, String name) {super();this.id = id;this.age = age;this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Student [age=" + age + ", id=" + id + ", name=" + name + "]";}}
package com.amaker.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;public class MyDefaultHandler extends DefaultHandler {private List<Student> studentList;private Student currentStudent;private String currentTag;@Overridepublic void startDocument() throws SAXException {super.startDocument();studentList = new ArrayList<Student>();}@Overridepublic void endDocument() throws SAXException {super.endDocument();}@Overridepublic void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {super.startElement(uri, localName, qName, attributes);if(qName!=null&&qName.equals("student")){currentStudent = new Student();int id = Integer.parseInt(attributes.getValue("id"));currentStudent.setId(id);}currentTag = qName;}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {super.endElement(uri, localName, qName);if(qName!=null&&qName.equals("student")){studentList.add(currentStudent);currentStudent = null;}currentTag = null;}@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {super.characters(ch, start, length);if(currentTag!=null){String data = new String(ch,start,length);if(currentTag.equals("name")){currentStudent.setName(data);}if(currentTag.equals("age")){currentStudent.setAge(Integer.parseInt(data));}}}public List<Student> getStudents(){return studentList;}}
package com.amaker.sax;import java.io.InputStream;import java.util.List;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.SAXException;public class Test {/** * @param args */public static void main(String[] args) {parse();}public static void parse(){InputStream in = new Test().getClass().getClassLoader().getResourceAsStream("stu.xml");SAXParserFactory factory = SAXParserFactory.newInstance();try {SAXParser parser= factory.newSAXParser();MyDefaultHandler handler = new MyDefaultHandler();parser.parse(in, handler);List<Student> list = handler.getStudents();for(Student s:list){System.out.println(s);}} catch(Exception e) {e.printStackTrace();}}}