我正在使用 SAX(XML 的简单 API)来解析 XML 文档。该文档是一个巨大的 XML 文件(dblp.xml - 1.46 GB),我编写了几行解析器并在小文件上进行了测试,它可以工作。
Sample.XML 和 Student.XML 是具有几行 XML 的小文件,我的解析器会解析它们,但是当我将路径更改为 dblp.XML 时,它会生成文件未找到异常(文件仍然与其他示例文件一起存在,但它的尺寸很大) 这是我得到的异常:
java.io.FileNotFoundException: E:\Workspaces\Java\SaxParser\xml\dblp.dtd (The system cannot find the file specified)
这是我的代码:
package com.teamincredibles.sax;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Parser extends DefaultHandler {
public void getXml() {
try {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
final MySet openingTagList = new MySet();
final MySet closingTagList = new MySet();
DefaultHandler defaultHandler = new DefaultHandler() {
public void startDocument() throws SAXException {
System.out.println("Starting Parsing...\n");
}
public void endDocument() throws SAXException {
System.out.print("\n\nDone Parsing!");
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (!openingTagList.contains(qName)) {
openingTagList.add(qName);
System.out.print("<" + qName + ">\n");
}
}
public void characters(char ch[], int start, int length)
throws SAXException {
/*for(int i=start; i<(start+length);i++){
System.out.print(ch[i]);
}*/
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (!closingTagList.contains(qName)) {
closingTagList.add(qName);
System.out.print("</" + qName + ">");
}
}
};
saxParser.parse("xml/dblp.xml", defaultHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
Parser readXml = new Parser();
readXml.getXml();
}
}
我不明白这是怎么回事。
最佳答案
您的 XML 文件是否引用 DTD,在本例中为“dblp.dtd”。
如果是,请检查其是否位于“E:\Workspaces\Java\SaxParser\xml\”位置。如果没有,请将其放在该位置并运行您的代码。
关于java - 在 Java 中解析大型 XML 文件时出现文件未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29360820/