I use XSL to configure my XML file into smaller XML. My code snippets are as follows:
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 the XSL file to convert the XML manually, I have no problem. However, with this JAVA file I cannot transform.
What is the problem?
source
share