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