Android XML file parsing-SAX

Source: Internet
Author: User
Tags tagname

Parsing of XML files saxThe following instance code mainly uses the Sax event-driven processing mechanism to parse the XML file, and the Sax parsing XML is done by a progressive scan, taking up little memory. Dom mechanism is to read all the files, and then in the form of tree structure, to complete the search, delete and other operations, memory consumption is large
Mycontenthandler. Java This file is an event handler implementation
Package Com.example.analysisxml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import Org.xml.sax.helpers.DefaultHandler;





Public class Mycontenthandler extends DefaultHandler {

String tagName;
String id,name,sex;

//Get the contents of the tag, label start and end of label, will call
@Override
public void characters (char[] ch, int start, int length)
throws saxexception {
//TODO auto-generated method stub
super.characters (CH, start, length);
//Prevent the label from continuing after the end of the Operation
if (tagName = = null)
return;
String str = new String (ch, start, length);
if (tagname.equals ("name")) {
name = str;
}
Else if (tagname.equals ("Sex")) {
sex = str;
}
//system.out.println ("tagName =" + TagName + "; str =" + name ");
}

//Call at end of document
@Override
public void Enddocument () throws saxexception {
//TODO auto-generated method stub
Super.enddocument ();
System.out.println ("-------------end------------");
}

called at end of//tag
@Override
public void EndElement (string uri, String localname, String qName)
throws saxexception {
//TODO auto-generated method stub
super.endelement (URI, LocalName, qName);
prevent calling the characters () method after the label finishes
TagName = null;
if (localname.equals ("worker")) {
System.out.print ("name =" + name);
System.out.print ("sex =" + Sex);
System.out.println ();
}

}

Document Start
@Override
public void Startdocument () throws saxexception {
//TODO auto-generated method stub
Super.startdocument ();

System.out.println ("-------------start------------");

}

//Label start
@Override
public void Startelement (string uri, String localname, String qName,
Attributes Attributes) throws saxexception {
//TODO auto-generated method stub
super.startelement (URI, LocalName, qName, attributes);
TagName = localname;
System.out.println ("tagName =" + TagName);
if (localname.equals ("worker")) {
for (int i = 0;i < Attributes.getlength (); i++) {
System.out.println (Attributes.getlocalname (i) + "=" + Attributes.getvalue (i));
}
}
}

}The following code is an implementation call to parse an XML stringstrxmlCreate a sax parsing factory
SAXParserFactory factory = Saxparserfactory.newinstance ();
XMLReader reader = Factory.newsaxparser (). Getxmlreader ();
Setting the event handler for XmlReader
Reader.setcontenthandler (New Mycontenthandler ());
Start parsing xml
Reader.parse (New InputSource (new StringReader (strxml)));

The format of the XML file is as follows:
<workers>
<worker id= "001" >
<name>Same</name>
<sex>man</sex>
</worker>
<worker id= "002" >
<name>
<sex>man</sex>
</worker>
<worker id= "002" >
<name>Rose</name>
<sex>man</sex>
</worker>
<worker id= "003" >
<name>
<sex>man</sex>
</worker>
<worker id= "003" >
<name>Jack</name>
<sex>man</sex>
</worker>
</workers>
<sex>man</sex>
</worker>
</workers>


Android XML file parsing-SAX

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.