You are here:   HomeJava ProgrammingPackagesSAXParser Example in Java
by Dinesh Thakur Category: Packages

 

import javax.xml.parsers.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.util.*;
import java.io.*;
public class JavaExampleSAXPARSER extends DefaultHandler
     {
         static String TxtToDisp[] = new String[1000];
         static int TxtLns = 0;
         static String Indnt = "";
         public static void parseDocument(String FileNme)
          {
             JavaExampleSAXPARSER SAXEvntHndlr = new JavaExampleSAXPARSER();
             SAXParserFactory PrsrFctry = SAXParserFactory.newInstance();
             XMLReader XmlRdr = null;
             try
                {
                   SAXParser SxPrsr = PrsrFctry.newSAXParser();
                   XmlRdr = SxPrsr.getXMLReader();
                }
                   catch (Exception e1)
                  {
                          System.err.println(e1);
                          System.exit(1);
                      }
                          XmlRdr.setContentHandler(SAXEvntHndlr);
                          XmlRdr.setErrorHandler(SAXEvntHndlr);
                          try
                        {
                                String Pth = new File(FileNme).getAbsolutePath();
                                XmlRdr.parse("file:" + Pth);
                            }
                           catch (SAXException e1)
                          {
                                     System.err.println(e1.getMessage());
                                     System.exit(1);
                                  }
                                     catch (IOException IOE)
                                {
                                             System.err.println(IOE);
                                             System.exit(1);
                                        }
          }
                             public void startDocument()
                             {
                                 TxtToDisp[TxtLns] = Indnt +"<?xml version=\"1.0\" encoding=\"UTF-8\"?>";TxtLns++;
                             } 
                                 public void startElement(String FlName, String LclNme, String QlifiedNme, Attributes Attrbt)
                                 {
                                     TxtToDisp[TxtLns] = Indnt + "<" +QlifiedNme;
                                     Indnt += "    ";
                                     if (Attrbt != null)
                                {
                                            int NmbrAttrbts = Attrbt.getLength();
                                            for (int loopIndx = 0;loopIndx<NmbrAttrbts;loopIndx++)
                                              {
                                                   TxtToDisp[TxtLns] += " ";
                                                   TxtToDisp[TxtLns] +=Attrbt.getLocalName(loopIndx);
                                                   TxtToDisp[TxtLns] += "=\"";
                                                   TxtToDisp[TxtLns] +=Attrbt.getValue(loopIndx);
                                                   TxtToDisp[TxtLns] += "\"";
                                              }
                                        }
                                           TxtToDisp[TxtLns] += ">";
                                           TxtLns++;
                                 }
                                           public void characters(char TxtCh[], int TxtStrt, int TxtLngth)
                                             {
                                                 String TxtData = (new String(TxtCh,TxtStrt,TxtLngth));
                                                 TxtData = TxtData.trim();
                                                  if(TxtData.length() > 0 && TxtData.indexOf("\n") < 0)
                                       {
                                                         TxtToDisp[TxtLns] = Indnt;
                                                         TxtToDisp[TxtLns] += TxtData;
                                                         TxtLns++;
                                                   }
                                             }
                                                   public void endElement(String FlName, String LclNme, String QlifiedNme)
                                                     {
                                                         Indnt = Indnt.substring(0, Indnt.length() - 4);
                                                         TxtToDisp[TxtLns] = Indnt + "</"+ QlifiedNme + ">";
                                                         TxtLns++;
                                                     }
                                                         public void processingInstruction(String PINAME, String PIDATA)
                                                         {
                                                             TxtToDisp[TxtLns] = Indnt + "<?" + PINAME;
                                                              if (PIDATA != null && PIDATA.length() > 0)
                                                     {
                                                                      TxtToDisp[TxtLns] += " ";
                                                                      TxtToDisp[TxtLns] += PIDATA;
                                                                 }
                                                                       TxtToDisp[TxtLns] += "?>";
                                                                       TxtLns++;
                                                         }
                                                              public void warning(SAXParseException e2)
                                                               {
                                                                   System.err.println("SAX WARNING" + e2.getMessage());
                                                               }
                                                                   public void error(SAXParseException e3)
                                                                     {
                                                                          System.err.println("SAX ERROR " +e3.getMessage());
                                                                          System.exit(1);
                                                                     }
                                                                          public void fatalError(SAXParseException e3) 
                                                                            {
                                                                                 System.err.println("FATAL SAX ERROR " +e3.getMessage());
                                                                                 System.exit(1);
                                                                            }
                                                                                 public static void main(String as[])
                                                                                      {
                                                                                            parseDocument(as[0]);
                                                                                            for(int indx = 0; indx < TxtLns; indx++)
                                                                            {
                                                                                                     System.out.println(TxtToDisp[indx]);
                                                                                                }
                                                                                      }
    }

SAXParser Example in Java





Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular