Convert a string to XML input stream in java

2019-03-11 02:46发布

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.

标签: java fop
3条回答
你好瞎i
2楼-- · 2019-03-11 02:55
new StreamSource(new StringReader(str))
查看更多
家丑人穷心不美
3楼-- · 2019-03-11 03:13

Use ByteArrayInputStream:

String S = ...;
InputStream source = new ByteArrayInputStream(S.getBytes(encoding))
查看更多
Deceive 欺骗
4楼-- · 2019-03-11 03:19

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.

查看更多
登录 后发表回答