java - Saxon XSLT Transform to String Instead of File -
in java code below, creating *.html report transforming generated xml data stored in string,
combinedddistring
... against xslt file,
reportxslt
... , writing result physical file,
tmpreporthtml
.
the code reads file string use in other methods. avoid having write results file, transform results directly string.
is there way can write transform results string directly, , avoid writing results out physical file?
string reportstring = null; fileinputstream stream = null; bytearrayoutputstream reportbaos = new bytearrayoutputstream(); try { system.setproperty("javax.xml.transform.transformerfactory", "net.sf.saxon.transformerfactoryimpl"); transformerfactory = transformerfactory.newinstance(); transformer = transformerfactory.newtransformer(new streamsource(reportxslt)); transformer.setoutputproperty(outputkeys.encoding, "us-ascii"); transformer.setoutputproperty(outputkeys.indent, "yes"); /* * create new report file time-stamped uniqueness, avoid concurrency issues */ dateformat dateformat = new simpledateformat("yyyymmddhhmmss"); date date = new date(); file tmpreporthtml = new file(reporthtml + dateformat.format(date) + ".html"); /* * perform transform report */ fileoutputstream reportfos = new fileoutputstream(tmpreporthtml); outputstreamwriter osw = new outputstreamwriter(reportfos, "us-ascii");//(reportbaos), "us-ascii"); transformer.transform(new streamsource(new stringreader(combinedddistring)), new streamresult(osw)); osw.close(); /* * report string temp report file */ //fileinputstream stream = new fileinputstream(new file(report_output)); stream = new fileinputstream(tmpreporthtml); //(new file(reportxml)); filechannel fc = stream.getchannel(); mappedbytebuffer bb = fc.map(filechannel.mapmode.read_only, 0, fc.size()); reportstring = charset.defaultcharset().decode(bb).tostring(); /* * delete temp report file */ tmpreporthtml.delete(); } catch (transformerconfigurationexception e) { e.printstacktrace(); } catch (exception ex) { ex.printstacktrace(); } { stream.close();
thanks in advance help.
try using stringwriter
create streamresult
. (you should able obtain string
stringwriter
using tostring
method.)
Comments
Post a Comment