XML parsing sax parsing:
SAX parser: SAXParser similar to Dom also under the Javax.xml.parsers package, instances of this class can be obtained from the Saxparserfactory.newsaxparser () method.
Note the parse () method of SAXParser:
Parse (String URI, DefaultHandler dh), Parse (File F, defaulthandler dh), etc. all need to pass a DefaultHandler object.
Looking at the API help manual, the SAX parsing is event-driven, and DefaultHandler is the default base class for the SAX2 event handler, which in the Org.xml.sax.helpers package implements callbacks for related events and provides a default empty implementation of all callback methods.
Common callback methods for DefaultHandler:
void Startdocument (): Receives notification of the beginning of a document.
void Startelement (String uri, String localname, String qName, Attributes Attributes): A notification that the receiving element starts.
void characters (char[] ch, int start, int length): Receives notification of character data in the element.
void EndElement (String uri, String localname, String qName): Notifies the end of the receiving element.
void Enddocument (): Notifies the end of a document.
Why does sax mean push mode parsing?
The SAX parser uses an event-based model that, when parsing an XML document, triggers a sequence of events that are automatically invoked by the parser, rather than by ourselves.
Use Sax to parse XML and observe various event callbacks:
There are the following XML files to parse:
1 <?xml version= "1.0" encoding= "UTF-8" standalone= "no"?> 2 3 <students> 4 5 <student id= " 003 "> 6 7 <name>xiaoqiao</name> 8 9 </student>10 </students>
Parsing code:
1 public class Saxxmlparser {2 3 public static void main (string[] args) {4 5 SAXPARSERFAC Tory factory = Saxparserfactory.newinstance (); 6 7 try {8 9 SAXParser parser = Factory.newsaxparser (); 10 11 Parser.parse ("Students_bak.xml", new DefaultHandler () {@Overrid E14 public void characters (char[] ch, int start, int length) 16 17 Throws Saxexception {SYSTEM.O Ut.println ("Read characters:" +new String (Ch,start,length)); 20 21}22 23 @Override24 public void Enddocument () throws Saxexception {System.out.println ("End document ..."); 28 29 }30 @Override32 33 public void EndElement (string uri, String localname, String qName) 34 35 Throws Saxexception {PNs System.out.println ("End element: < "+qname+" > ")}40 @Override42 43 public void Startdocument () throws Saxexception {44 45 SYSTEM.OUT.PRINTLN ("Start document ..."); 46 47}48 49 @Override50 public void Startelement (string uri, String Loca LName, String qName, Attributes Attributes), throws Saxexcepti on {54 55 System.out.println ("Start element:<" +qname+ ">"); 56 57 if (qname.equals ("student")) {System.out.pri Ntln ("id=" +attributes.getvalue ("id")); 60 61}62 63 }64}); (Exception e) {68 69 E.printstacktrace (); 70 71}72 73}74 75}
The above code in the parse () method, the second parameter defines an anonymous inner class object that inherits from DefaultHandler, overriding 5 Main event callback methods to observe the event's triggering process.
In the Startelement () method, QName holds the node name, attributes is the attribute of the node, and the EndElement () method is the same.
Operation Result:
Start Document ...
Start element:<students>
Read characters:
Start element:<student>
id=003
Read characters:
Start element:<name>
Read Characters:xiaoqiao
End Element:<name>
Read characters:
End Element:<student>
Read characters:
End Element:<students>
End Document ...
The first callback is the Startdocument () method;
Then it touches the <students> node, callback Startelement () method;
<students> rear carriage return, so the characters () method will be triggered to output the currently read characters;
Then the <student> node is encountered, callback Startelement () method, output id attribute value;
There is also a carriage return, so the characters () method is triggered to output the currently read character;
And then encountered the <name> node, callback Startelement () method;
Back is Xiaoqiao, callback characters () method;
then to </name>, callback EndElement () method;
Hit Enter, callback characters () method;
to </student>, callback EndElement () method;
Hit Enter, callback characters () method;
to </students>, callback EndElement () method;
End of document, callback Enddocument () method, parse work end.
If you need to do something else with the document, such as finding a node, you can override the response's event callback method and add the implementation code for the desired logic.
XML parsing (ii) SAX parsing