java 文件过早结束错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6291577/
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
Premature end of file Error
提问by gencay
I am using XSL to configure my XML file into a smaller XML. My code fragments are so:
我正在使用 XSL 将我的 XML 文件配置为较小的 XML。我的代码片段是这样的:
public class MessageTransformer {
public static void main(String[] args) {
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer (new StreamSource("sample.xsl"));
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(new StreamSource ("sample.xml"),
new StreamResult( new FileOutputStream("sample.xml"))
);
}
catch (Exception e) {
e.printStackTrace( );
}
}
}
I got this error
我收到这个错误
ERROR: 'Premature end of file.'
ERROR: 'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Premature end of file.'
When I use XSL file to transform XML manually I don' t have any problem. However with this JAVA file I cannot transform.
当我使用 XSL 文件手动转换 XML 时,我没有任何问题。但是使用这个 JAVA 文件我无法转换。
What would be the problem?
会有什么问题?
回答by wjans
You are streaming from and to the same file. Try changing it to something like this:
您正在从同一个文件流式传输和流式传输到同一个文件。尝试将其更改为如下所示:
transformer.transform(new StreamSource ("sample.xml"),
new StreamResult( new FileOutputStream("sample_result.xml"))
);
回答by NIrav Modi
Complete your xml file give any tag other wise it will give an error: Premature end of file.
完成你的 xml 文件给任何标签,否则它会给出一个错误:文件过早结束。
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
<Customer>
<person>
<Customer_ID>1</Customer_ID>
<name>Nirav Modi</name>
</person>
<person>
<Customer_ID>2</Customer_ID>
<name>Nihar dave</name>
</person>
</Customer>
Like this and try again.
像这样,然后再试一次。