无法将 xmlReader 转换为解析器
Cannot cast xmlReader to Parser
我正在按照 Maven 项目的 this 教程尝试使用 SAX 解析 XML 文件。这是我的 SAXLocalNameCount
:
public class SAXLocalNameCount extends DefaultHandler {
private static String filename = ".\file.xml";
public static void main(String[] args) throws ParserConfigurationException, org.xml.sax.SAXException, IOException, SAXException {
SAXParserFactory spfactory = SAXParserFactory.newInstance();
spfactory.setNamespaceAware(true);
SAXParser saxParser = spfactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new SAXLocalNameCount());
xmlReader.parse(convertToFileURL());
}
private static void usage() {
System.err.println("Usage: SAXLocalNameCount <file.xml>");
System.err.println(" -usage or -help = this message");
System.exit(1);
}
private Hashtable tags;
public void startDocument() throws SAXException {
tags = new Hashtable();
}
public void startElement(String namespaceURI,
String localName,
String qName,
Attributes atts)
throws SAXException {
String key = localName;
Object value = tags.get(key);
if (value == null) {
tags.put(key, new Integer(1));
}
else {
int count = ((Integer)value).intValue();
count++;
tags.put(key, new Integer(count));
}
}
public void endDocument() throws SAXException {
Enumeration e = tags.keys();
while (e.hasMoreElements()) {
String tag = (String) e.nextElement();
int count = (Integer) tags.get(tag);
System.out.println("Local Name \"" + tag + "\" occurs "
+ count + " times");
}
}
private static String convertToFileURL() {
String path = new File(filename).getAbsolutePath();
if (File.separatorChar != '/') {
path = path.replace(File.separatorChar, '/');
}
if (!path.startsWith("/")) {
path = "/" + path;
}
return "file:" + path;
}
}
这一行:
XMLReader xmlReader = saxParser.getXMLReader();
给我错误:
ClassCastException: org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser 无法转换为 jdk.internal.org.xml.sax.XMLReader
我在这里错过了什么?
我的问题是我从 jdk.internal.org.xml.sax.helpers.DefaultHandler 导入,正确的导入应该是 org.xml.sax.helpers.DefaultHandler
我正在按照 Maven 项目的 this 教程尝试使用 SAX 解析 XML 文件。这是我的 SAXLocalNameCount
:
public class SAXLocalNameCount extends DefaultHandler {
private static String filename = ".\file.xml";
public static void main(String[] args) throws ParserConfigurationException, org.xml.sax.SAXException, IOException, SAXException {
SAXParserFactory spfactory = SAXParserFactory.newInstance();
spfactory.setNamespaceAware(true);
SAXParser saxParser = spfactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new SAXLocalNameCount());
xmlReader.parse(convertToFileURL());
}
private static void usage() {
System.err.println("Usage: SAXLocalNameCount <file.xml>");
System.err.println(" -usage or -help = this message");
System.exit(1);
}
private Hashtable tags;
public void startDocument() throws SAXException {
tags = new Hashtable();
}
public void startElement(String namespaceURI,
String localName,
String qName,
Attributes atts)
throws SAXException {
String key = localName;
Object value = tags.get(key);
if (value == null) {
tags.put(key, new Integer(1));
}
else {
int count = ((Integer)value).intValue();
count++;
tags.put(key, new Integer(count));
}
}
public void endDocument() throws SAXException {
Enumeration e = tags.keys();
while (e.hasMoreElements()) {
String tag = (String) e.nextElement();
int count = (Integer) tags.get(tag);
System.out.println("Local Name \"" + tag + "\" occurs "
+ count + " times");
}
}
private static String convertToFileURL() {
String path = new File(filename).getAbsolutePath();
if (File.separatorChar != '/') {
path = path.replace(File.separatorChar, '/');
}
if (!path.startsWith("/")) {
path = "/" + path;
}
return "file:" + path;
}
}
这一行:
XMLReader xmlReader = saxParser.getXMLReader();
给我错误:
ClassCastException: org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser 无法转换为 jdk.internal.org.xml.sax.XMLReader
我在这里错过了什么?
我的问题是我从 jdk.internal.org.xml.sax.helpers.DefaultHandler 导入,正确的导入应该是 org.xml.sax.helpers.DefaultHandler