Xml Parsing method for androidsax

Source: Internet
Author: User
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 methods for the ContentHandler Interface: The first method for parsing sax is described below:

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.
The 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) 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 file string content, and the next two parameters are the start position and length of the read string in this array, use new String (ch, start, length) to obtain the content.

The xml source file is the same as the xml file in the android dom parsing xml method. you can refer to the file for parsing, but the name is different,

In this example, three saxTest. xml files are displayed, which correspond to three different reading methods.

// Read the xml file under the class package // InputStream inputStream = // this. getClass (). getClassLoader (). getResourceAsStream ("cn/com/sax/saxTest. xml "); // read the xml file InputStream inputStream under src = this. getClass (). getClassLoader (). getResourceAsStream ("saxTest. xml "); // read the xml file under assets // InputStream inputStream = this. getAssets (). open ("saxTest. xml ");
The first method to parse xml is mainly to inherit DefaultHandler and implement several methods

The source code for my parsing will be released below:

/*** The first method for parsing xml with sax * @ return */private String saxParse () {SAXParserFactory factory = SAXParserFactory. newInstance (); SAXParser parser; String xmlStr = "first resolution method: \ n"; try {// read the xml file under the class package // InputStream inputStream = // this. getClass (). getClassLoader (). getResourceAsStream ("cn/com/sax/saxTest. xml "); // read the xml file InputStream inputStream under src = this. getClass (). getClassLoader (). getResourceAsStream ("saxTest. xml "); // read the xml file under assets // InputStream inputStream = this. getAssets (). open ("saxTest. xml "); parser = factory. newSAXParser (); SaxParseHandler personHandler = new SaxParseHandler (); parser. parse (inputStream, personHandler); xmlStr = xmlStr + personHandler. parseToString ();} catch (ParserConfigurationException e) {e. printStackTrace (); Log. e ("TEST", "" + e. toString ();} catch (SAXException e) {e. printStackTrace (); Log. e ("TEST", "" + e. toString ();} catch (IOException e) {e. printStackTrace (); Log. e ("TEST", "" + e. toString ();} return xmlStr ;}

Inherit DefaultHandler to process element nodes in xml.

Class SaxParseHandler extends DefaultHandler {String result; public String parseToString () {return result;} String str = null;/*** start with xml, run this function */@ Override public void startDocument () throws SAXException {// Log. e ("TEST", "startDocument");}/*** When xml ends, execute this function */@ Override public void endDocument () throws SAXException {// Log. e ("TEST", "endDocument");}/*** when the xml tag starts, run this function to read the tag name, and Attributes in the tag */@ Override public void startElement (String uri, String localName, String qName, attributes Attributes) throws SAXException {if ("group ". equals (localName) {String groupName = attributes. getValue (0); String num = attributes. getValue (1); result = result + "groupName =" + groupName + "num =" + num + "\ n";} else if ("person ". equals (localName) {String personName = attributes. getValue (0); String age = attributes. getValue (1); // equivalent to getValue (1) // String txt = attributes. getValue ("age"); result = result + "personName" + personName + "age =" + age + "\ n";} else {str = localName ;} // Log. e ("TEST", "startElement");}/*** when the xml tag ends, execute this function */@ Override public void endElement (String uri, String localName, string qName) throws SAXException {str = null; // Log. e ("TEST", "endElement");}/*** resolve the value in the tag, as shown in
 
  
Ssss
 To read "ssss" */@ Override public void characters (char [] ch, int start, int length) throws SAXException {if (str! = Null) {String data = new String (ch, start, length); // When formatting an xml file, the following special characters such as line breaks and tabs are easily generated, // pay special attention to processing Pattern p = Pattern. compile ("\ s * | \ t | \ r | \ n"); Matcher m = p. matcher (data); data = m. replaceAll (""); if ("chinese ". equals (str) {if (! Data. equals ("") {String chinese = data; result = result + "chinese =" + chinese ;}} else if ("english ". equals (str) {if (! Data. equals ("") {String english = data; result = result + "english =" + english + "\ n ";}}}}}

The above content is the xml Parsing method for android sax. For more information, see PHP Chinese website (www.php1.cn )!

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.