The Sax-Simple API for XML is simply a set of sun-provided API mechanisms that manipulate XML data.
The parsing principle used by Sax is based on the mechanism of event triggering.
Sax technology can only be read by XML data.
1. Prepare XML file to parse Linkmans.xml
<?xml version= " 1.0 " encoding=" UTF-8 standalone=" no ?><linkmans> <linkman> <name>jack</name& Gt <phone>18663243245 </phone> <email>[email protected] 163 .com</email> </linkman> <linkman> <name> Zhang San < ;/name> <phone>1353243247 </phone> <EMAIL>[EMAIL&NB Sp;protected]126 .com</email> </linkman></linkmans>
2 // 2. Get the SAX parser public static SAXParser Getparser () Throws exception{ 2.1 get Parser factory class object
saxparserfactory factory = Saxparserfactory.newinstance (); SAXParser parser = Factory.newsaxparser (); return parser; }
3 . Parse data // 3. Parse data public staticvoid getelement (file file, DefaultHandler dh) throws exception{ // 3.1 Get parser saxparser parser = Getparser (); // 3.2 Calling the Parse method parser.parse (file, DH); }
4. Create a Myhandler.java that inherits from the DefaultHandler class Public classMyHandler extends DefaultHandler {//automatically executes when the document starts parsing Public voidstartdocument () throws Saxexception {super.startdocument (); System. out. println ("XML document start parsing ..."); } //encountered a start element automatic execution Public voidstartelement (String uri, String localname, string name, Attributes Attributes) throws Saxexception { Super.startelement (URI, LocalName, name, attributes); System. out. println ("Elements"+name+"start ..."); } //encountered text content automatic execution Public voidCharacters (Char[] ch,intStartintlength) throws saxexception {super.characters (ch, start, length); System. out. println ("text content"+NewString (ch,start,length)); } //encountered element end automatic execution Public voidendElement (String uri, String localname, String name) throws Saxexception {super.endelement (URI, L Ocalname, name); System. out. println ("Elements"+name+"End ..."); } //custom execution When document parsing is complete Public voidenddocument () throws Saxexception {super.enddocument (); System. out. println ("XML document parsing end ..."); }}
5 methods for invoking parsing elements in the main function Public Static void Main (string[] args) throws Exception { new File ("linkmans.xml" ); New MyHandler (); GetElement (File,handler); }
Through the above procedures found that in fact, sax parsing only requires the developer to pass the corresponding event handler object (inherit from the DefaultHandler Class).
Example 1: Create an event handler to get the specified node data from the XML. Public classOptiondatahandler extends DefaultHandler {//defines the name of a member variable record label PrivateString name =NULL; //A variable that defines a statistic number Private intCount =0; //encountered a start element automatic execution Public voidstartelement (String uri, String localname, string name, Attributes Attributes) throws Saxexception { Super.startelement (URI, LocalName, name, attributes); This. Name =name; if("name". Equals ( This. Name)) { This. count++; } } //encountered text content automatic execution Public voidCharacters (Char[] ch,intStartintlength) throws saxexception {super.characters (ch, start, length); //determine if the name element if("name". Equals ( This. Name) && This. Count = =2) {System. out. println (NewString (ch,start,length)); } }}
Case one: Parsing XML data with sax and encapsulating the collection of data
In fact, the use of any XML parsing technology to parse XML data requires later data encapsulation, if it is only a separate to get the value of a node does not have any meaning. It is therefore necessary to parse the XML data so that it is encapsulated into the specified collection as an object to facilitate the processing of other applications.
1. Implement a Linkman.java JavaBean object Public classLinkman {PrivateString name; PrivateString Phone; PrivateString Email; PublicLinkman () {super (); } PublicLinkman (string name, string phone, string email) {super (); This. Name =name; This. Phone =phone; This. email =email; } //omit get and set methods}
3. Create an event handler for encapsulation Listhandler.java Public classListhandler extends DefaultHandler {//Defining JavaBean Objects PrivateLinkman Linkman =NULL; //defining a collection encapsulates a JavaBean object Privatelist<linkman> list =NULL; //defines the name of the parsed element PrivateString name =NULL; @Override Public voidstartdocument () throws Saxexception {super.startdocument (); //to create an object of the list collection This. List =NewArraylist<linkman>(); } @Override Public voidstartelement (String uri, String localname, string name, Attributes Attributes) throws Saxexception { Super.startelement (URI, LocalName, name, attributes); //record the name of the parsed element This. Name =name; //if it's linkman, then create a Linkman object if("Linkman". Equals ( This. Name)) { This. Linkman =NewLinkman (); }} @Override Public voidCharacters (Char[] ch,intStartintlength) throws saxexception {super.characters (ch, start, length); //if the element name is name at this time get text value if("name". Equals ( This. Name)) { This. Linkman.setname (NewString (ch,start,length)); } //if the element name is phone, get text value if("Phone". Equals ( This. Name)) { This. Linkman.setphone (NewString (ch,start,length)); } //if the element name is an email get text value if("Email". Equals ( This. Name)) { This. Linkman.setemail (NewString (ch,start,length)); }} @Override Public voidendElement (String uri, String localname, String name) throws Saxexception {super.endelement (URI, L Ocalname, name); //release THIS.name This. Name =NULL; //if the element name at this time is Linkman if("Linkman". Equals (name)) { //to add a Linkman object to a collection This. List.add ( This. Linkman); //emptying the Linkman object This. Linkman =NULL; } } //provide a way to get the packaged Linkman collection PublicList<linkman>getList () {returnlist; }}
Summary: It must be a good choice if you use Sax to read data, but this parsing technique cannot do other things such as: Add, delete, and change.
Solution: There can be a technology award DOM and sax combine to efficiently query and perform actual data manipulation (add, delete, and change). Even with third-party XML parsing techniques dom4j.
Javase Learning Notes Sax parsing (6)