How to parse xml files using sax in Android

Source: Internet
Author: User
Tags xml parser

SAX is an xml parser with high resolution speed and low memory usage. It is suitable for Android and other mobile devices. An event-driven file is used to parse an XML file. That is to say, it does not need to parse a complete file. In the process of parsing a document in order of content, SAX checks whether the currently read characters are valid for a part of the XML syntax. If yes, the event is triggered. The so-called events are actually some callback methods, which are defined in the ContentHandler interface. Below are some common ContentHandler interface methods:

StartDocument (): When a document starts, call this method to perform preprocessing.

EndDocument (): corresponds to the preceding method. When the document ends, you can call this method to complete the work.

StartElement (String namespaceURI, String localName, String qName, Attributes atts)

This method is triggered when a start tag is read. NamespaceURI is the namespace, localName is the tag name without the namespace prefix, and qName is the tag name with the namespace prefix. You can use atts to obtain all the attribute names and corresponding values. It should be noted that an important feature of SAX is its stream processing. When a tag is encountered, it will not record the previously encountered tag, that is, in startElement () all the information you know in the method is the name and attribute of the tag. As for the nested structure of the tag, the name of the Upper-layer tag, whether there is sub-meta, and other information related to the structure, they are all unknown, and they all need your program to complete. This makes it easier to program and process SAX than DOM.

EndElement (String uri, String localName, String name): This method corresponds to the preceding method. This method is called when an end tag is encountered.

Characters (char [] ch, int start, int length): This method is used to process the content read in the XML file. The first parameter is the string content of the file, the following two parameters are the start position and length of the read String in this array. You can obtain the content using new String (ch, start, length.

Note: When a tag contains many characters, or a string contains special characters such as "\ n", character loss occurs. In this case, do not doubt that the SAX cannot parse the tag containing "\ n" or XML with many characters. In fact, Handler we write is problematic. When parsing XML, when a tag contains a large amount of content, SAX will call characters multiple times. So we need to consider this situation when writing Handler. It won't lose characters. The following is a solution: create a temporary variable temp, call the characters method to use temp to receive data, and assign the Temporary Variable temp to the target variable in the endElement method, and empty the Temporary Variable temp to avoid dirty data. This solves the problem that the content after the line break symbol cannot be read when the sax reads XML.

Ljq. xml file
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Persons>
<Person id = "23">
<Name> Li Ming </name>
<Age> 30 </age>
</Person>
<Person id = "20">
<Name> Li xiangmei </name>
<Age> 25 </age>
</Person>
</Persons>

Entity class
Copy codeThe Code is 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 codeThe Code is 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 {
// Temporary variable to solve the problem that the content after the line break symbol cannot be read by sax when reading XML; 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 codeThe Code is 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 {
// The business layer needs to be thrown out.
Public static List <Person> readXML (InputStream inStream) throws Exception {
SAXParserFactory spf = SAXParserFactory. newInstance ();
SAXParser saxParser = spf. newSAXParser (); // create a parser
// Set the parser related properties, http://xml.org/sax/features/namespaces = true
// Enable the namespace feature
// 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 codeThe Code is 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 {
// Put ljq. xml 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 ());
}
}
}

Running result

In the android simulator, if it is a Chinese character, garbled characters will not appear on the real machine.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.