XML is a common data transmission method. Therefore, during development, we will encounter parsing XML files. This article describes how to use the SAM parser to parse XML files.
The file format to be parsed is as follows:
liming
30
lixiangmei
25
Below is our entity class
public class Person {private Integer id;private String name;private Short age;public Person(){}public Person(Integer id, String name, Short age) {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 Short getAge() {return age;}public void setAge(Short age) {this.age = age;}@Overridepublic String toString() {return "Person [age=" + age + ", id=" + id + ", name=" + name + "]";}}
We define a SAM parser to parse the content
/*** Use SAX to parse XML files ** @ author zhaokaiqiang **/public class SaxResolve {public List
GetPersons (InputStream inputStream) throws Exception {// get the SAXParser object SAXParser parser = SAXParserFactory. newInstance (). newSAXParser (); // instantiate our parser PersonHandler handler = new PersonHandler (); // start parsing parser for the input stream. parse (inputStream, handler); // close the inputStream of the input stream. close (); // return result return handler. getPersons ();} private class PersonHandler extends DefaultHandler {private List
Persons; private Person person = null; private String tag = null; public List
GetPersons () {return persons;} // when the start part of the xml file is read, initialize persons @ Overridepublic void startDocument () throws SAXException {persons = new ArrayList
();} // This method parses element nodes, such
,,
All belong to the element node // when the person node is read, the Person object is initialized, and the value of the attribute node is obtained and assigned to the id, // record the name of the current node, that is, localName, used for parsing the node inside the Person node @ Overridepublic void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {if (localName. equals ("person") {person = new Person (); person. setId (new Integer (attributes. getValue (0);} tag = localName;} // This callback method is used to parse text nodes, such as 3030 in it is a text node. // based on the value of the element node tag, we extract the text node and assign a value to person @ Overridepublic void characters (char [] ch, int start, int length) throws SAXException {if (tag! = Null) {String data = new String (ch, start, length); if ("name ". equals (tag) {person. setName (data);} else if ("age ". equals (tag) {person. setAge (new Short (data) ;}}/** when an element node is reached, for example *
*
Lixiangmei
* 25*
* When you arrive here
In this callback method, the parsed person object * is added to the persons and the person and tag are initialized, parse the next person node */@ Overridepublic void endElement (String uri, String localName, String qName) throws SAXException {if (localName. equals ("person") {persons. add (person); person = null;} tag = null ;}}}
Because the annotations are detailed, there are only a lot of parsing. Next we will use unit testing to test the parsing.
// Test SAX parsing public void testSax () throws Exception {// obtain the input stream InputStream inputStream = getClass () from the xml file (). getClassLoader (). getResourceAsStream ("persons. xml "); // obtain the parsed Object List
Persons = new SaxResolve (). getPersons (inputStream); // print for (Person p: persons) {Log. d (TAG, p. toString ());}}
The resolution result is as follows: