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.FactoryC onfigurati onError: Provider org.apache.xerces.jaxp.SAX ParserFact oryImpl could not be instantiated: java.lang.NullPointerExcep tion
at javax.xml.parsers.SAXParse rFactory.n ewInstance (SAXParser Factory.ja va:113)
at ParseXML.parseTheXml(Parse XML.java:1 46)
at ParseXML.main(ParseXML.jav a: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("Parsin g begins...");
}
public void endDocument( ) throws SAXException {
System.out.println("...Par sing 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 ("accessio n") && attributes.getValue(i).equ als(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.SAXPars erFactory" ;
if (System.getProperty(jaxpPr opertyName ) == null) {
String apacheXercesPropertyValue =
"org.apache.xerces.jaxp.SA XParserFac toryImpl";
System.setProperty(jaxpPro pertyName,
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.newInstan ce();
try {
SAXParser parser = factory.newSAXParser();
parser.parse(xmlFile, handler);
} catch(Exception e) {
String errorMessage =
"Error parsing " + xmlFile + ": " + e;
System.err.println(errorMe ssage);
e.printStackTrace();
}
}
}//ends class
************************** ********** ********** **
Exception in thread "main" javax.xml.parsers.FactoryC
at javax.xml.parsers.SAXParse
at ParseXML.parseTheXml(Parse
at ParseXML.main(ParseXML.jav
*********************
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("Parsin
}
public void endDocument( ) throws SAXException {
System.out.println("...Par
}
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
{
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.SAXPars
if (System.getProperty(jaxpPr
String apacheXercesPropertyValue =
"org.apache.xerces.jaxp.SA
System.setProperty(jaxpPro
apacheXercesPropertyValue)
}
if (args.length == 2) {
xmlFile = args[0];
inputFile=args[1];
outputFile=inputFile+"_" +"GOAnnotated";
leftOutFile=inputFile+"_" +"Unannotated";
} else {
System.out.println("Usage:
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.newInstan
try {
SAXParser parser = factory.newSAXParser();
parser.parse(xmlFile, handler);
} catch(Exception e) {
String errorMessage =
"Error parsing " + xmlFile + ": " + e;
System.err.println(errorMe
e.printStackTrace();
}
}
}//ends class
**************************
You may not have that one
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)