2

I came across the below example in one of the stackoverflow posts -

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

public class TestMain {
    public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        Source xslt = new StreamSource(new File("transform.xslt"));
        Transformer transformer = factory.newTransformer(xslt);

        Source text = new StreamSource(new File("input.xml"));
        transformer.transform(text, new StreamResult(new File("output.xml")));
    }
}

In the above example , I would like to use input as a string and output as string and read only xslt from file. Is it possible to achieve this?

Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
  • 1
    Possible duplicate of [Convert a string to XML input stream in java](http://stackoverflow.com/questions/1510712/convert-a-string-to-xml-input-stream-in-java) – Stobor Mar 15 '17 at 03:55

2 Answers2

3

Have look at the StreamSource constructor with InputStream https://docs.oracle.com/javase/7/docs/api/javax/xml/transform/stream/StreamSource.html#StreamSource(java.io.InputStream)

You can create your input from string like this:

    InputStream input = new ByteArrayInputStream("<your string input>".getBytes(StandardCharsets.UTF_8));
    Source text = new StreamSource(input);

And get your output as string using StringWriter and StreamResult:

    StringWriter outputWriter = new StringWriter();
    StreamResult result = new StreamResult( outputWriter );

    transformer.transform( text, result );  

    String outputAsString = outputWriter.getBuffer().toString(); 
mjlowky
  • 1,183
  • 12
  • 19
0

I was able to do something similar to this which provided me the result -

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;

public class TestMain {
    public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        Source xslt = new StreamSource(new File("transform.xslt"));

        Transformer transformer = factory.newTransformer(xslt);
String xmlData = "<xml><data>test</data>";
InputStream stream = new ByteArrayInputStream(xmlData.getBytes(StandardCharsets.UTF_8));
        Source text = new StreamSource(stream);

            StringWriter outWriter = new StringWriter();
            StreamResult result = new StreamResult( outWriter );
        transformer.transform(text, result);
            StringBuffer sb = outWriter.getBuffer(); 
            String finalstring = sb.toString();
    }
}
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315