Org.xml.sax.SAXParseException: premature final file

I currently have the following XMLfile.

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

My XMLParser.javaclass.

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 search, but I keep getting the above error.

Does anyone know what the problem is?

Thank you for your help.

+4
source share
1 answer

Typically, "org.xml.sax.SAXParseException: invalid target file" occurs for several reasons.

1.Check your xml, all tags were closed correctly at the same level

2. Check to see if any namespace has arisen.

3.check XML-

+2

Source: https://habr.com/ru/post/1535223/


All Articles