2

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.

LeDerp
  • 573
  • 2
  • 9
  • 24
  • 1
    Can you show us your `SaxHandler` implementation? – millhouse May 18 '15 at 00:27
  • You can find it in the following link: http://tutorials.jenkov.com/java-xml/sax-example.html it's the same, – LeDerp May 18 '15 at 00:28
  • I'd be worried about `jdk.internal.org.xml.sax`... – MadProgrammer May 18 '15 at 00:29
  • I think you actually want `javax.xml.parsers.SAXParser` – MadProgrammer May 18 '15 at 00:31
  • Are you developing without an IDE? Half of these dependencies are unused. Also, copying form the tutorial works for me, so you must have done something differently. – Deltharis May 18 '15 at 00:33
  • Yep - I think your IDE has auto-imported the wrong class(es) for `SAXException` and/or `DefaultHandler` – millhouse May 18 '15 at 00:33
  • I'm using Netbeans IDE, my IDE imported the classes automatically, should I copy the imports from the websites and try ? – LeDerp May 18 '15 at 00:41
  • @Deltharis Could you please tell which IDE you are using – LeDerp May 18 '15 at 00:45
  • IntelliJ at the moment, though any IDE should work - see http://stackoverflow.com/questions/956900/code-cleanup-in-netbeans for Netbeans. Also, yes, if you imported the dependencies manually (especially for your `SaxHandler` implementation which seems to be the problem) you should try to revert to the ones in the tutorial, you might have gotten the wrong classes there. – Deltharis May 18 '15 at 00:52
  • Waaaait a second. Did you put everything from the tutorial into a single file? Is that why Netbeans put those dependencies there? In that case @MadProgrammer was correct in his (now deleted) answer – Deltharis May 18 '15 at 00:55
  • No, I made 4 classes, driver.java, veichle.java, SaxHandler.java, SaxParserExample.java – LeDerp May 18 '15 at 01:00
  • Do you have the libraries of a parser implementation (i.e. Xerces, Saxon, ....) correctly set in your classpath? – potame May 18 '15 at 06:58

1 Answers1

4

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

LeDerp
  • 573
  • 2
  • 9
  • 24