Sax parsing:Sax parses an XML file in an event-handling manner, using Sax to parse an XML document, involving two parts:parser and event handlers:1. The parser can be created using the Saxp API, and after creating the SAX parser, you can specify the parser to parse an XML document. When parsing an XML document in sax mode, the parser will call a method of the event handler whenever it resolves to an XML document, and the parser will pass the current parsed XML file contents as parameters of the method to the event handler when invoking the event handler's method. 2. The event handler is written by the programmer, and the programmer can easily get the data parsed by the SAX parser through the parameters of the method in the event handler, so that it can decide how to process the data. Unlike DOM, the SAX parser does not parse the XML directly but creates Saxreader to parse the XML
Ideas:Use SAXParserFactory to create a sax parser factory parserfactory = Saxparserfactory.newinstance ();//To get the parser object parser through the SAX parser factory = Parserfactory.newsaxparser ();//Get an XML file reader through the parser object XmlReader reader = Parser.getxmlreader ();// Set the reader's event handler Reader.setcontenthandler (New Userhandler ());//parse XML file Reader.parse ("Src/users.xml");
main methods of processor interface:Startdocument Start document Enddocument end document Startelement start element endelement end element characters get text
Code:
1SAXParserFactory saxparserfactory =saxparserfactory.newinstance ();2SAXParser parser =Saxparserfactory.newsaxparser ();3XMLReader reader =Parser.getxmlreader ();4Reader.setcontenthandler (NewContentHandler () {5 //Start document node6 Public voidStartdocument ()throwssaxexception {7SYSTEM.OUT.PRINTLN ("Start document Node");8 }9 Ten //End Document Node One Public voidEnddocument ()throwssaxexception { ASYSTEM.OUT.PRINTLN ("End document Node"); - } - the //Start element Node - @Override - Public voidstartelement (String uri, string localname, -String QName, Attributes atts)throwssaxexception { +System.out.println ("Start element node" +qName); - //get attribute actions for an element +String data = Atts.getvalue (0); A } at - //End Element Node - @Override - Public voidendElement (String uri, String localname, string qName) - throwssaxexception { -SYSTEM.OUT.PRINTLN ("End element Node" +qName); in } - to //Get text + @Override - Public voidCharacters (Char[] ch,intStartintlength) the throwssaxexception { *System.out.println (NewString (CH, start, length)); $ }Panax Notoginseng - //There are several methods, not commonly used, empty implementations ..... the }); +Reader.parse ("Bookstore.xml");
using interfaces to implement so many methods is inefficient, so Java provides the DefaultHandler class
as long as the required method of replication:
1Reader.setcontenthandler (NewDefaultHandler () {2 @Override3 Public voidCharacters (Char[] ch,intStartintlength)4 throwssaxexception {5System.out.println (NewString (CH, start, length));6 }7});
6. Sax Parsing xml