org.xml.sax.SAXParseException: Premature end file

user2268507 picture user2268507 · Apr 6, 2014 · Viewed 31.6k times · Source

I currently have the following XML file.

http://www.cse.unsw.edu.au/~cs9321/14s1/assignments/musicDb.xml

My XMLParser.java class.

package edu.unsw.comp9321.assignment1;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

public class XMLParser {


public void search () {

    try{
        File fXmlFile = new File("/COMP9321Assignment1/xml/musicDb.xml");           
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

I create an object in another class and call the search but I keep receiving the above stated error.

Would anyone know what the problem might be?

Thanks for your help.

Answer

Naveen picture Naveen · Apr 6, 2014

Normally "org.xml.sax.SAXParseException: Premature end file" occurrs due to several reasons

1.Check your xml were all the tags are closed properly at same level

2.check if any name space issues.

3.check for welformness of your xml document