gpt4 book ai didi

java - 无法在 SAXParser 中解析文件和处理程序

转载 作者:行者123 更新时间:2023-12-01 11:38:30 26 4
gpt4 key购买 nike

我正在向 SAXParser 传递一个 XML 文件和一个处理程序,但收到此错误: Cannot resolve method 'parse(java.io.File, jdk.internal.org.xml.sax.helpers.DefaultHandler)'

parse 方法的属性定义为 (File, DefaultHandler) ,它完全匹配,所以我不确定哪里出错了。完整方法如下:

public String readXML (File readFile) throws Exception {
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxFactory.newSAXParser();
final String outputString = "";
DefaultHandler handler = new DefaultHandler() {
boolean bArtist = false;
boolean bAlbumName = false;
boolean bYear = false;
boolean bGenre = false;

public void startElement(String uri, String localName, String qName, Attributes attr)
throws SAXException {
if (qName.equalsIgnoreCase("ARTIST")) { bArtist = true; }
if (qName.equalsIgnoreCase("ALBUMNAME")) { bAlbumName = true; }
if (qName.equalsIgnoreCase("YEAR")) { bYear = true; }
if (qName.equalsIgnoreCase("GENRE")) { bGenre = true; }
}
public void characters(char ch[], int start, int length) {
if (bArtist) {
outputString.concat("Artist: " + new String(ch,start,length) + "\n");
}
if(bAlbumName) {
outputString.concat("Album: " + new String(ch,start,length) + "\n");
}
if(bYear) {
outputString.concat("Year: " + new String(ch,start,length) + "\n");
}
if(bGenre) {
outputString.concat("Genre: " + new String(ch,start,length) + "\n");
}
outputString.concat("\n");
}
};

saxParser.parse(readFile,handler);
return outputString;
}

最佳答案

它应该是org.xml.sax.helpers.DefaultHandler不是jdk.internal...

所以

import org.xml.sax.helpers.DefaultHandler;

您可能还应该定义扩展 DefaultHandler 的类,而不仅仅是内联它。

关于java - 无法在 SAXParser 中解析文件和处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29753235/

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