Sax is an XML parser that is fast and consumes less memory, and is ideal for mobile devices such as Android. The Sax parsing XML file takes event-driven, that is, it does not need to parse through the entire document, and in the process of parsing the document in content order, sax determines whether the currently-read character is legitimate in the XML syntax, and triggers the event if it is compliant. The so-called events, in fact, are some callback (callback) methods, these methods (events) are defined in the ContentHandler interface. Here are some common methods for ContentHandler interfaces:
Startdocument (): When you encounter the beginning of a document, call this method, you can do some preprocessing work.
Enddocument (): Corresponds to the above method, when the document is finished, call this approach, you can do some cleanup work.
Startelement (String NamespaceURI, String LocalName, String qName, Attributes atts)
This method is triggered when a start tag is read. NamespaceURI is a namespace, LocalName is a label name without a namespace prefix, QName is a label name with a namespace prefix. All the property names and corresponding values can be obtained by Atts. Note that one of the important features of Sax is its flow processing, when a tag is encountered, it does not record the previously encountered tags, that is, in the Startelement () method, all you know is the name and attributes of the tag, as for the nested structure of the tag, The name of the top tag, whether there are any other structure-related information, etc., are unknown and require your program to complete. This makes it so easy for sax to have no DOM on the programming process.
EndElement (String uri, String localname, String name): This method corresponds to the above square method, which is called when the end tag is encountered.
Characters (char[] ch, int start, int length): This method is used to process the content that is read in the XML file, the first parameter is the string content of the file, and the next two parameters are the starting position and length of the string to read in the array, using the new String (ch,start,length) to get the content.
Note: Character loss occurs when there are a few characters between a label, or if a string contains special characters such as "\ n". Do not suspect that Sax cannot parse XML that contains "\ n" or more characters in the tag. In fact, the handler we write is problematic. When sax parses XML, it calls characters more than once when it encounters a label that has a lot of content. So we have to take this into account when writing handler. You won't lose the characters. Here's a solution: Create a temporary variable temp, use temp to receive data in the call characters method, assign temp to the target variable in the EndElement method, and empty temporary variable temp to avoid dirty data. This resolves the problem when sax reads XML without reading the content after the newline symbol.
Ljq.xml file
Copy Code code as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<persons>
<person id= ">"
<name> Li Ming </name>
<age>30</age>
</person>
<person id= ">"
<name> Li Xiangmei </name>
<age>25</age>
</person>
</persons>
Entity classes
Copy Code code as follows:
Package com.ljq.entity;
Import java.io.Serializable;
/**
* Entity class
*
* @author Jiqinlin
*
*/
@SuppressWarnings ("Serial")
public class person implements serializable{
Private Integer ID;
private String name;
private short age;
Public person () {
}
Public person (String name, short age) {
Super ();
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;
}
}
Handle class
Copy Code code as follows:
Package com.ljq.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.ljq.entity.Person;
public class Xmlcontenthandler extends DefaultHandler {
A temporary variable that solves the problem where sax reads XML without reading the contents of the newline symbol; note that temp= "" cannot be written as temp or temp=null to avoid dirty data
Private String temp= "";
Private String Pretag;
private person person;
private list<person> persons;
@Override
public void Startdocument () throws Saxexception {
persons = new arraylist<person> ();
}
@Override
public void characters (char[] ch, int start, int length) throws Saxexception {
if (person!=null) {
String date = new String (ch,start,length);
if ("Name". Equals (Pretag)) {
Temp+=date;
}else if ("Age". Equals (Pretag)) {
Temp+=date;
}
}
}
@Override
public void Startelement (string uri, String localname, string name, Attributes Attributes) throws Saxexception {
if ("Person". Equals (LocalName)) {
person= new Person ();
Person.setid (Attributes.getindex ("id"));
}
Pretag = LocalName;
}
@Override
public void EndElement (string uri, String localname, String name) throws Saxexception {
if (person!=null && name. Equals (LocalName)) {
Person.setname (temp);
Temp= "";
}else if (person!=null && ' age '. Equals (LocalName)) {
Person.setage (new short (temp));
Temp= "";
}else if (person!=null && ' person ". Equals (LocalName)) {
Persons.add (person);
person = null;
}
Pretag = null;
}
Getter ...
Public list<person> getpersons () {
return persons;
}
}
Copy Code code as follows:
Package com.ljq.service;
Import Java.io.InputStream;
Import java.util.List;
Import Javax.xml.parsers.SAXParser;
Import Javax.xml.parsers.SAXParserFactory;
Import Com.ljq.entity.Person;
/**
* Sax Resolution Factory
*
* @author Jiqinlin
*
*/
public class Saxpersonservice {
Business layer to be thrown outside
public static list<person> ReadXML (InputStream instream) throws Exception {
SAXParserFactory SPF = saxparserfactory.newinstance ();
SAXParser saxparser = Spf.newsaxparser (); Creating a parser
Sets the correlation attribute of the parser, http://xml.org/sax/features/namespaces = True
Indicates open namespace attribute
Saxparser.setproperty ("Http://xml.org/sax/features/namespaces", true);
Xmlcontenthandler handler = new Xmlcontenthandler ();
Saxparser.parse (instream, handler);
Instream.close ();
return Handler.getpersons ();
}
}
Test class
Copy Code code as follows:
Package com.ljq.test;
Import Java.io.InputStream;
Import java.util.List;
Import Com.ljq.entity.Person;
Import Com.ljq.service.SaxPersonService;
Import Android.test.AndroidTestCase;
Import Android.util.Log;
public class Saxpersonservicetest extends androidtestcase{
Private final String TAG = "Saxpersonservicetest";
public void Testreadxml () throws exception{
Ljq.xml placed in the SRC directory
InputStream InputStream = SaxPersonServiceTest.class.getClassLoader (). getResourceAsStream ("Ljq.xml");
list<person> persons = Saxpersonservice.readxml (InputStream);
for (person Person:persons) {
LOG.I (TAG, Person.getid () + ":" + person.getname () + ":" + person.getage ());
}
}
}
Run results
In the Android emulator, if the Chinese character will appear garbled, this will not happen on the real machine