0

I am trying to generate outFile base on the marshalled xml and xslt and some issue.

Piece of code produces xml stream from object.

 JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeFormat.class);
 ByteArrayOutputStream os = new ByteArrayOutputStream();
 Marshaller marshaller = jaxbContext.createMarshaller();
 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);         
 marshaller.marshal(employeeFormat,os);
 outEmpFile(new ByteArrayInputStream(os.toByteArray())); 

This method produces the outFile taking the xml as input

public void outEmpFile(ByteArrayInputStream inputStream) throws IOException {
    File template = new File("C/workspace/files/Employee.xslt"); 
    File outFile = new File(C:/workspace/files/Employee.java");

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer(new StreamSource(template));             
    transformer.transform(new StreamSource(inputStream),new StreamResult(outFile));
}

I get TransformerException when running the code :

 ERROR:  'Premature end of file.'javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: 

  com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Premature end of file.

Both input xml and xslt produces expected output in online tool - http://www.utilities-online.info/xsltransformation.

This code gives the correct xml file in console.

public void  byteToString(ByteArrayInputStream is) {       
    int size = is.available();
    char[] theChars = new char[size];
    byte[] bytes    = new byte[size];

 is.read(bytes, 0, size);
    for (int i = 0; i < size;)
        theChars[i] = (char)(bytes[i++]&0xff);

    System.out.println("Xml String :"+ new String(theChars));
  }

Any help on the issue please?

Aniket V
  • 3,183
  • 2
  • 15
  • 27
jack
  • 803
  • 3
  • 15
  • 26

1 Answers1

0

The issue is the inputstream is consumed before passing in transform method which resulted in Premature end of file error. Refer this for more info - org.xml.sax.SAXParseException: Premature end of file for *VALID* XML

jack
  • 803
  • 3
  • 15
  • 26