javasaxsaxparsersaxparseexception

Sax Parser No method found to parse


Tried to run the code mentioned in the following tutorial Everything compiles perfectly but it shows the error in the main method.

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.jar.Attributes;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import jdk.internal.org.xml.sax.SAXException;
import jdk.internal.org.xml.sax.helpers.DefaultHandler;


public class SaxParserExample {

public static void main (String argv []) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    try {
        InputStream    xmlInput  =
            new FileInputStream("data\\sax-example.xml");

        SAXParser      saxParser = factory.newSAXParser();
        SaxHandler handler   = new SaxHandler();
        saxParser.parse(xmlInput, handler);

        for(Driver driver : handler.drivers){
            System.out.println(driver);
        }
    } catch (Throwable err) {
        err.printStackTrace ();
    }
}

}

on the following line:

saxParser.parse(xmlInput, handler);

Error:

error: no suitable method found for parse(InputStream,SaxHandler)
            saxParser.parse(xmlInput, handler);
    method SAXParser.parse(InputStream,HandlerBase) is not applicable
      (argument mismatch; SaxHandler cannot be converted to HandlerBase)
    method SAXParser.parse(InputStream,DefaultHandler) is not applicable
      (argument mismatch; SaxHandler cannot be converted to DefaultHandler)
    method SAXParser.parse(String,HandlerBase) is not applicable
      (argument mismatch; InputStream cannot be converted to String)
    method SAXParser.parse(String,DefaultHandler) is not applicable
      (argument mismatch; InputStream cannot be converted to String)
    method SAXParser.parse(File,HandlerBase) is not applicable
      (argument mismatch; InputStream cannot be converted to File)
    method SAXParser.parse(File,DefaultHandler) is not applicable
      (argument mismatch; InputStream cannot be converted to File)
    method SAXParser.parse(InputSource,HandlerBase) is not applicable
      (argument mismatch; InputStream cannot be converted to InputSource)
    method SAXParser.parse(InputSource,DefaultHandler) is not applicable
      (argument mismatch; InputStream cannot be converted to InputSource)

As you can see I have already included necessary files yet it doesn't compile.


Solution

  • Changing the imports from

    import jdk.internal.org.xml.sax.Attributes;
    import jdk.internal.org.xml.sax.SAXException;
    import jdk.internal.org.xml.sax.helpers.DefaultHandler;
    

    to

    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
    

    solves the error