我正在使用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();
}
}
}
当我使用XSL文件来转换XML手动我不有任何问题,我得到这个错误
ERROR: 'Premature end of file.'
ERROR: 'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Premature end of file.'
。但是,这个JAVA文件无法转换。
会出现什么问题?
我觉得让StreamSource(你原来的xml)和StreamResult(xslt后的结果)指向同一个文件,可能会让你陷入困境。 – Wivani 2011-06-09 10:53:58