开发者

Premature end of file Error

I am using XSL to configure my XML开发者_运维问答 file into a smaller XML. My code fragments are so:

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.

What would be the problem?


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"))
    );


Complete your xml file give any tag other wise it will give an error: Premature end of 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 this and try again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜