Link to home
Start Free TrialLog in
Avatar of micro_learner
micro_learner

asked on

Java SAX parser Exception : Provider org.apache.xerces.jaxp.SAXParserFactoryImpl could not be instantiated:

Hello ..I am trying to implement a Java SAX parser for my application but I am encountering a SAX error when I am instantiating  the parser itself ..could you help me wth this ,..

Exception in thread "main" javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.SAXParserFactoryImpl could not be instantiated: java.lang.NullPointerException
        at javax.xml.parsers.SAXParserFactory.newInstance(SAXParserFactory.java:113)
        at ParseXML.parseTheXml(ParseXML.java:146)
        at ParseXML.main(ParseXML.java:122)



*********************
import org.xml.sax.*;
import org.xml.sax.helpers.*;

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;



public class ParseXML extends DefaultHandler{
      
      public static String geneid="";
      
      
      public void startDocument(  ) throws SAXException {
        System.out.println("Parsing begins...");
      }


      public void endDocument(  ) throws SAXException {
        System.out.println("...Parsing ends.");
}
      
      public void startElement(String namespaceUri,
            String localName,
            String qualifiedName,
            Attributes attributes) throws SAXException {
            
            
             for (int i=0; i<attributes.getLength(  ); i++)
             {
                 // System.out.println("  Attribute: " + attributes.getLocalName(i) +
                 //                    "=" + attributes.getValue(i));          
      
                   if(attributes.getLocalName(i).equals("accession") && attributes.getValue(i).equals(geneid))

                   {
                         System.out.println("I was sucessful in the first part\n");
                         System.out.println("This geneId is found "+geneid);
                         System.exit(1);
                   }
                  
             }
      
      
      }
            
            
      
      
      
      
      public void endElement(String namespaceUri,
            String localName,
            String qualifiedName) throws SAXException {}
      


      public void characters(char[] chars,
            int startIndex,
            int endIndex) {}

      
      
      
      public static void main(String[] args) {
        
            String xmlFile="";
            String inputFile="";
            String outputFile="";
            String leftOutFile ="";
            String line="";
            
            String jaxpPropertyName =
            "javax.xml.parsers.SAXParserFactory";
          
          if (System.getProperty(jaxpPropertyName) == null) {
            String apacheXercesPropertyValue =
              "org.apache.xerces.jaxp.SAXParserFactoryImpl";
            System.setProperty(jaxpPropertyName,
                               apacheXercesPropertyValue);
          }
          
        
          if (args.length == 2) {
                xmlFile = args[0];
                inputFile=args[1];
                outputFile=inputFile+"_" +"GOAnnotated";
                leftOutFile=inputFile+"_" +"Unannotated";
                
          } else {
            
                System.out.println("Usage: java ParseXML [xml file] [geneidfile]");
                System.exit(1);
          
          }
        
      
          // Get the ID from the input file
          
          try
          {
                 BufferedReader br = new BufferedReader(
                        new InputStreamReader(new FileInputStream(inputFile)));
                
                 line = br.readLine(); // Assumes the first Line has the Header
                 int count =0;
                  while ((line = br.readLine()) != null) {
                    
                      if(line!="")
                      {
                            count++;
                            line=line.trim();
                            geneid=line;
                            //Send the ID to the XML parser and parse the file
                            System.out.println("This is id "+count + geneid);
                            parseTheXml(line,xmlFile);
                            if(count==20)
                            {
                                  System.exit(1);
                            }
                      }
                      else
                            continue;
                        
                  }
                
          }catch(Exception e)
          {
                e.printStackTrace();
          }
          
          
      
      }//ends main
      
        
        public static void parseTheXml(String geneId,String xmlFile)
        {
              DefaultHandler handler = new ParseXML();
              SAXParserFactory factory = SAXParserFactory.newInstance();
             
              try {
                  SAXParser parser = factory.newSAXParser();
                  parser.parse(xmlFile, handler);
                } catch(Exception e) {
                  String errorMessage =
                    "Error parsing " + xmlFile + ": " + e;
                  System.err.println(errorMessage);
                  e.printStackTrace();
                }
             
        }


}//ends class
************************************************
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You may not have that one
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)