gpt4 book ai didi

java - Sax Parser 找不到可解析的方法

转载 作者:行者123 更新时间:2023-11-30 08:11:47 25 4
gpt4 key购买 nike

尝试运行下面提到的代码tutorial一切都完美编译,但它显示了 main 方法中的错误。

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 ();
}
}

}

在下面一行:

saxParser.parse(xmlInput, handler);

错误:

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)

如您所见,我已经包含了必要的文件,但它无法编译。

最佳答案

更改导入

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

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

解决错误

关于java - Sax Parser 找不到可解析的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30293503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com