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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5894025/
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-10-30 13:19:06  来源:igfitidea点击:

org.xml.sax.SAXParseException: Premature end of file

javaxmlxml-parsingsax

提问by Cyril Deba

this is my xml file

这是我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<settings></settings>

and my method is shown below

我的方法如下所示

public  void load( String fileName ) {
...
Document xmlDocument = null;
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try
{
  builder = dbFactory.newDocumentBuilder();
  xmlDocument = builder.parse(new File (fileName));
}
catch( Exception e )
{
  Log.write(e);
}
...
}

When I invoke my method I receive the following:

当我调用我的方法时,我收到以下信息:

[task] org.xml.sax.SAXParseException: Premature end of file.
[task] at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:249)
[task] at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
[task] at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
[task] at MyClass.load(MyClass.java:49)

I am completely confused.

我完全糊涂了。

Thanks for any ideas.

感谢您的任何想法。

-Cyril

-西里尔

回答by anamika

problem may exist in struts.xml file. The following line must be included in struts.xml file.

struts.xml 文件中可能存在问题。以下行必须包含在 struts.xml 文件中。

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

and <package>tag must be written inside <struts>tag inside sturts.xml file.

<package>标签必须写<struts>在 sturts.xml 文件中的标签内。

回答by Qstacker

Make sure the content type content-type:text/xml; charset=utf-8

确保内容类型 content-type:text/xml; charset=utf-8