Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Transformer: How do you make its result into an OutputStream?

I am new to javax.xml.transform.Transformer.

I am applying an XSLT on an XML document and It works fine.

What I want to achieve is to be able to write the output of that tranformation to an OutputStream.

This is my code:

OutputStream outputStream = null;
InputStream agent = new FileInputStream("src/res/testxmlfile.xml");
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource("src/res/trans.xslt"));
transformer.transform(new StreamSource(agent), outputStream ????????);

I know it can be used to write a file like this, but I want to write it to a OutputStream Object.

transformer.transform(new StreamSource(agent),
                      new StreamResult(new FileOutputStream("/result.xml")));

How can I pass an OutputStream to be used here?

This is the error I am getting when I am passing the Outputstream:

Exception in thread "main" javax.xml.transform.TransformerException:
                            Result object passed to ''{0}'' is invalid.
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl
                           .getOutputHandler(TransformerImpl.java:468)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl
                           .transform(TransformerImpl.java:344)
at com.gohealth.TestXmlStream.main(TestXmlStream.java:75)
like image 732
Aniks Avatar asked Sep 25 '14 19:09

Aniks


People also ask

What are the methods of OutputStream in Java?

Methods of OutputStream. 1 write () - writes the specified byte to the output stream. 2 write (byte [] array) - writes the bytes from the specified array to the output stream. 3 flush () - forces to write all data present in output stream to the destination. 4 close () - closes the output stream.

Why can't I create an object of OutputStream in Java?

It is because OutputStream is an abstract class, so we cannot create an object of OutputStream. Note: We can also create the output stream from other subclasses of the OutputStream class. The OutputStream class provides different methods that are implemented by its subclasses.

What is a fileoutputstream in Java?

As the name suggests, a FileOutputStream is an OutputStream to write data to a File. FileOutputStream, like any other OutputStream, can write a stream of raw bytes. We have already examined different methods in FileOutputStream as part of the last section. 5.2. ByteArrayOutputStream

What is OutputStream in C++?

OutputStream is an abstract class that enables you to write data to an output sink or destination. As with InputStream, that destination could be a console, a file, a socket, a pipe and even a buffer in memory. The most basic and fundamental thing you can do with an OutputStream is write a sequence of bytes to it.


2 Answers

Use a StreamResult. It provides constructors to write to a File or an OutputStream:

Example using File:

transformer.transform(new StreamSource(agent), new StreamResult(file));

Example using FileOutputStream:

FileOutputStream outputStream = new FileOutputStream(new File("outputfile.xml"));
transformer.transform(new StreamSource(agent), new StreamResult(outputStream));

Example using ByteArrayOutputStream:

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
transformer.transform(new StreamSource(agent), new StreamResult(outputStream));
byte[] bytes = outputStream.toByteArray();`
like image 162
M A Avatar answered Sep 30 '22 05:09

M A


Use a "StreamResult" constructed with an object that represents where you want the output. See http://docs.oracle.com/javase/7/docs/api/javax/xml/transform/stream/StreamResult.html

like image 24
brindy Avatar answered Sep 30 '22 04:09

brindy