2015-03-31 99 views
0

我正在使用SAX(简单API for XML)解析XML文档。该文件是一个巨大的XML文件(dblp.xml - 1.46 GB),我写了几行解析器,并在小文件上测试它,它工作。 Here is my projects hierarchy在Java中解析大型XML文件时未发现文件异常

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

什么是我想不通的事情。

回答

1

您的XML文件是否引用DTD,在本例中为“dblp.dtd”。

如果是,请检查它是否位于“E:\ Workspaces \ Java \ SaxParser \ xml \”位置。如果没有放置在位置并运行你的代码。

+0

感谢它的工作,但请解释什么是这个文件,我是这个领域的新手。 – 2015-03-31 05:01:37

+1

DTD定义了XML的结构。即可以在e.t.c中存在的标签和属性。基本上,这是你的xml的一个规则集,用于检查它是否合格。 http://en.wikipedia.org/wiki/Document_type_definition – Balaji 2015-03-31 05:12:27

+1

好的,非常感谢你,我懂了。 它的工作,但之后,它产生了另一个问题,请回答这个问题http://stackoverflow.com/questions/29360962/limit-imposed-by-the-application-while-parsing-xml – 2015-03-31 05:15:53