Java parses XML files using Sax

Source: Internet
Author: User
The following code can be run directly, hoping to save time for beginners who want to use SAX to parse XML files

Java code
  1. PackageParsexml;
  2. ImportOrg. Apache. xerces. parsers. saxparser;
  3. ImportOrg. xml. Sax. attributes;
  4. ImportOrg. xml. Sax. helpers. defaulthandler;
  5. // Directly parse the first method
  6. Public ClassSaxdemoExtendsDefaulthandler {
  7. Public Static VoidMain (string [] ARGs ){
  8. Try{
  9. Saxparser parser =
  10. (Saxparser) class. forname ("org. Apache. xerces. parsers. saxparser"). newinstance ();
  11. Saxdemo handler =NewSaxdemo ();
  12. Parser. setcontenthandler (handler );
  13. Parser. parse ("parsexml/test. xml ");
  14. }Catch(Exception e ){
  15. System. Out. println (E. tostring ());
  16. }
  17. }
  18. Public VoidStartelement (string Uri, string localname, string QNAME, attributes ATTR ){
  19. System. Out. println ("start uri of element:" + URI + "| localname:" + localname + "| QNAME:" + QNAME );
  20. If(ATTR. getlength ()> 0 ){
  21. For(IntI = 0; I <ATTR. getlength (); I ++ ){
  22. System. Out. println (ATTR. getqname (I) + "=" + ATTR. getvalue (I ));
  23. }
  24. }
  25. }
  26. Public VoidEndelement (string Uri, string localname, string QNAME ){
  27. System. Out. println ("End URI of element:" + URI + "localname:" + localname + "QNAME:" + QNAME );
  28. System. out. println ("========================================== =================================== ");
  29. }
  30. }
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

  1. PackageParsexml;
  2. ImportOrg. Apache. xerces. parsers. saxparser;
  3. ImportOrg. xml. Sax. attributes;
  4. ImportOrg. xml. Sax. saxexception;
  5. ImportOrg. xml. Sax. helpers. attributesimpl;
  6. ImportOrg. xml. Sax. helpers. defaulthandler;
  7. ImportOrg. xml. Sax. helpers. xmlfilterimpl;
  8. // Method 2: Use filter to parse
  9. Public ClassSaxdemo2ExtendsDefaulthandler {
  10. Public Static VoidMain (string [] ARGs ){
  11. Try{
  12. // Generate saxparser
  13. Saxparser parser =
  14. (Saxparser) class. forname ("org. Apache. xerces. parsers. saxparser"). newinstance ();
  15. // Generate Handler
  16. Saxdemo2 handler =NewSaxdemo2 ();
  17. Filterhandler filter =NewFilterhandler ();
  18. // Configure the handler and parser just generated into the filter
  19. Filter. setparent (parser );
  20. Filter. setcontenthandler (handler );
  21. Filter. parse ("parsexml/test. xml ");
  22. }Catch(Exception e ){
  23. System. Out. println (E. tostring ());
  24. }
  25. }
  26. Public VoidStartelement (string Uri, string localname, string QNAME, attributes ATTR ){
  27. System. Out. println ("start uri of element:" + URI + "| localname:" + localname + "| QNAME:" + QNAME );
  28. If(ATTR. getlength ()> 0 ){
  29. For(IntI = 0; I <ATTR. getlength (); I ++ ){
  30. System. Out. println (ATTR. getqname (I) + "=" + ATTR. getvalue (I ));
  31. }
  32. }
  33. }
  34. Public VoidEndelement (string Uri, string localname, string QNAME ){
  35. System. Out. println ("End URI of element:" + URI + "localname:" + localname + "QNAME:" + QNAME );
  36. System. out. println ("========================================== =================================== ");
  37. }
  38. Static ClassFilterhandlerExtendsXmlfilterimpl {
  39. IntIndex = 0;
  40. Public VoidStartelement (string Uri, string localname, string QNAME, attributes ATTR)ThrowsSaxexception {
  41. Attributesimpl attrimpl =NewAttributesimpl (ATTR );
  42. For(IntI = 0; I <attrimpl. getlength (); I ++ ){
  43. If(Attrimpl. getqname (I). touppercase (). Equals ("ID ")){
  44. Attrimpl. removeattribute (I );
  45. Break;
  46. }
  47. }
  48. Attrimpl. addattriof ("", "ID", String. valueof (index ++ ));
  49. Super. Startelement (Uri, localname, QNAME, attrimpl );
  50. }
  51. Public VoidEndelement (string Uri, string localname, string QNAME)ThrowsSaxexception {
  52. Super. Endelement (Uri, localname, QNAME );
  53. }
  54. Public VoidCharacters (Char[] CH,IntIstart,IntIlength)ThrowsSaxexception {
  55. Super. Characters (CH, istart, ilength );
  56. }
  57. }
  58. }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.