Convert a string to XML input stream in java

て烟熏妆下的殇ゞ 提交于 2019-12-04 08:05:07

问题


I'm trying to generate a PDF document using FOP and Java.

I receive the XML as a string and not as a file.

How can I convert this XML string to an XML input stream so that I can call xslfoTransformer.transform(source, res); where source is my XML string as an Input stream.

Please provide your suggestions.


回答1:


You probably want to convert it to a Reader, not an InputStream. Use StringReader to do this. StreamSource has a constructor that takes a Reader, and you can pass that StreamSource to Transformer.transform().

I say you probably want a Reader rather than an InputStream because a String holds characters, not bytes, and an InputStream is a stream of bytes while a Reader is a stream of characters.




回答2:


new StreamSource(new StringReader(str))



回答3:


Use ByteArrayInputStream:

String S = ...;
InputStream source = new ByteArrayInputStream(S.getBytes(encoding))


来源:https://stackoverflow.com/questions/1510712/convert-a-string-to-xml-input-stream-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!