The following code can be run directly, hoping to save time for beginners who want to use SAX to parse XML files
Java code
- PackageParsexml;
- ImportOrg. Apache. xerces. parsers. saxparser;
- ImportOrg. xml. Sax. attributes;
- ImportOrg. xml. Sax. helpers. defaulthandler;
- // Directly parse the first method
- Public ClassSaxdemoExtendsDefaulthandler {
- Public Static VoidMain (string [] ARGs ){
- Try{
- Saxparser parser =
- (Saxparser) class. forname ("org. Apache. xerces. parsers. saxparser"). newinstance ();
- Saxdemo handler =NewSaxdemo ();
- Parser. setcontenthandler (handler );
- Parser. parse ("parsexml/test. xml ");
- }Catch(Exception e ){
- System. Out. println (E. tostring ());
- }
- }
- Public VoidStartelement (string Uri, string localname, string QNAME, attributes ATTR ){
- System. Out. println ("start uri of element:" + URI + "| localname:" + localname + "| QNAME:" + QNAME );
- If(ATTR. getlength ()> 0 ){
- For(IntI = 0; I <ATTR. getlength (); I ++ ){
- System. Out. println (ATTR. getqname (I) + "=" + ATTR. getvalue (I ));
- }
- }
- }
- Public VoidEndelement (string Uri, string localname, string QNAME ){
- System. Out. println ("End URI of element:" + URI + "localname:" + localname + "QNAME:" + QNAME );
- System. out. println ("========================================== =================================== ");
- }
- }
Package parsexml; import Org. apache. xerces. parsers. saxparser; import Org. XML. sax. attributes; import Org. XML. sax. helpers. defaulthandler; // The first method directly parses public class saxdemo extends defaulthandler {public static void main (string [] ARGs) {try {saxparser parser = (saxparser) class. forname ("org. apache. xerces. parsers. saxparser "). newinstance (); saxdemo handler = new saxdemo (); parser. setcontenthandler (handler); parser. parse ("parsexml/test. XML ");} catch (exception e) {system. out. println (E. tostring () ;}} public void startelement (string Uri, string localname, string QNAME, attributes ATTR) {system. out. println ("element start URI:" + URI + "| localname:" + localname + "| QNAME:" + QNAME); If (ATTR. getlength ()> 0) {for (INT I = 0; I <ATTR. getlength (); I ++) {system. out. println (ATTR. getqname (I) + "=" + ATTR. getvalue (I) ;}} public void endelement (string Uri, string localname, string QNAME) {system. out. println ("End URI of element:" + URI + "localname:" + localname + "QNAME:" + QNAME); system. out. println ("========================================== ==================================== ");}}
Java code
- PackageParsexml;
- ImportOrg. Apache. xerces. parsers. saxparser;
- ImportOrg. xml. Sax. attributes;
- ImportOrg. xml. Sax. saxexception;
- ImportOrg. xml. Sax. helpers. attributesimpl;
- ImportOrg. xml. Sax. helpers. defaulthandler;
- ImportOrg. xml. Sax. helpers. xmlfilterimpl;
- // Method 2: Use filter to parse
- Public ClassSaxdemo2ExtendsDefaulthandler {
- Public Static VoidMain (string [] ARGs ){
- Try{
- // Generate saxparser
- Saxparser parser =
- (Saxparser) class. forname ("org. Apache. xerces. parsers. saxparser"). newinstance ();
- // Generate Handler
- Saxdemo2 handler =NewSaxdemo2 ();
- Filterhandler filter =NewFilterhandler ();
- // Configure the handler and parser just generated into the filter
- Filter. setparent (parser );
- Filter. setcontenthandler (handler );
- Filter. parse ("parsexml/test. xml ");
- }Catch(Exception e ){
- System. Out. println (E. tostring ());
- }
- }
- Public VoidStartelement (string Uri, string localname, string QNAME, attributes ATTR ){
- System. Out. println ("start uri of element:" + URI + "| localname:" + localname + "| QNAME:" + QNAME );
- If(ATTR. getlength ()> 0 ){
- For(IntI = 0; I <ATTR. getlength (); I ++ ){
- System. Out. println (ATTR. getqname (I) + "=" + ATTR. getvalue (I ));
- }
- }
- }
- Public VoidEndelement (string Uri, string localname, string QNAME ){
- System. Out. println ("End URI of element:" + URI + "localname:" + localname + "QNAME:" + QNAME );
- System. out. println ("========================================== =================================== ");
- }
- Static ClassFilterhandlerExtendsXmlfilterimpl {
- IntIndex = 0;
- Public VoidStartelement (string Uri, string localname, string QNAME, attributes ATTR)ThrowsSaxexception {
- Attributesimpl attrimpl =NewAttributesimpl (ATTR );
- For(IntI = 0; I <attrimpl. getlength (); I ++ ){
- If(Attrimpl. getqname (I). touppercase (). Equals ("ID ")){
- Attrimpl. removeattribute (I );
- Break;
- }
- }
- Attrimpl. addattriof ("", "ID", String. valueof (index ++ ));
- Super. Startelement (Uri, localname, QNAME, attrimpl );
- }
- Public VoidEndelement (string Uri, string localname, string QNAME)ThrowsSaxexception {
- Super. Endelement (Uri, localname, QNAME );
- }
- Public VoidCharacters (Char[] CH,IntIstart,IntIlength)ThrowsSaxexception {
- Super. Characters (CH, istart, ilength );
- }
- }
- }