Invalid end of file. Error

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?

+3
source share
2 answers

You go from one file to the same file. Try changing it to something like this:

transformer.transform(new StreamSource ("sample.xml"),  
        new StreamResult( new FileOutputStream("sample_result.xml"))
    );
+6
source

Complete your XML file, give any tag to the other wise, it will give an error: Premature end of the file.

<?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 it and try again.

0
source

All Articles