Java org.xml.sax.SAXParseException:过早的结束文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22891337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:45:24  来源:igfitidea点击:

org.xml.sax.SAXParseException: Premature end file

javaxmldomparser

提问by

I currently have the following XMLfile.

我目前有以下XML文件。

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

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

My XMLParser.javaclass.

我的XMLParser.java课。

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 searchbut I keep receiving the above stated error.

我在另一个类中创建了一个对象并调用了 ,search但我一直收到上述错误。

Would anyone know what the problem might be?

有谁知道可能是什么问题?

Thanks for your help.

谢谢你的帮助。

回答by Naveen

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

通常“org.xml.sax.SAXParseException: Premature end file”由于多种原因发生

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

1.检查您的xml是否所有标签都在同一级别正确关闭

2.check if any name space issues.

2.检查是否有任何名称空间问题。

3.check for welformness of your xml document

3.检查你的xml文档的完整性