0

I'm using SAX (Simple API for XML) to parse an XML document. The document is a huge XML file (dblp.xml - 1.46 GB), i wrote a few lines of parser and tested it on small files and it works. Here is my projects hierarchy

Sample.XML and Student.XML are small files having few lines of XML, my parser parses them but when i change the path to dblp.XML it generates the file not found exception (file is still there with other sample files, but its huge in size) here is the Exception i get:

java.io.FileNotFoundException: E:\Workspaces\Java\SaxParser\xml\dblp.dtd (The system cannot find the file specified)

here is my code:

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

What is the matter i can't figure out.

Subhan
  • 1,544
  • 3
  • 25
  • 58

1 Answers1

1

Is your XML file referencing a DTD, in this case "dblp.dtd".

If yes check if its in the location "E:\Workspaces\Java\SaxParser\xml\". If not place it in the location and run your code.

Balaji
  • 1,009
  • 7
  • 21
  • Thanks it worked but please explain what is this file, i'm a newbie in this field. – Subhan Mar 31 '15 at 05:01
  • 1
    DTD defines the structure of your XML. i.e tags and attributes that can be present e.t.c. Basically, this is a ruleset for your xml that checks if its well formed or not. http://en.wikipedia.org/wiki/Document_type_definition – Balaji Mar 31 '15 at 05:12
  • 1
    Okay thank you so much i got it. it worked but after that it generated another problem, please answer that too http://stackoverflow.com/questions/29360962/limit-imposed-by-the-application-while-parsing-xml – Subhan Mar 31 '15 at 05:15