[英]Convert a string to XML input stream in java
I'm trying to generate a PDF document using FOP and Java. 我正在尝试使用FOP和Java生成PDF文档。
I receive the XML as a string and not as a file. 我将XML作为字符串而不是文件接收。
How can I convert this XML string to an XML input stream so that I can call xslfoTransformer.transform(source, res); 如何将此XML字符串转换为XML输入流,以便我可以调用xslfoTransformer.transform(source,res); where source is my XML string as an Input stream. 其中source是我的XML字符串作为输入流。
Please provide your suggestions. 请提供您的建议。
new StreamSource(new StringReader(str))
You probably want to convert it to a Reader
, not an InputStream
. 您可能希望将其转换为Reader
,而不是InputStream
。 Use StringReader to do this. 使用StringReader执行此操作。 StreamSource has a constructor that takes a Reader, and you can pass that StreamSource
to Transformer.transform(). StreamSource有一个构造函数,它接受一个Reader,你可以将StreamSource
传递给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. 我说你可能想要一个Reader
而不是一个InputStream
因为String包含字符而不是字节,而InputStream
是一个字节流,而Reader
是一个字符流。
Use ByteArrayInputStream : 使用ByteArrayInputStream :
String S = ...;
InputStream source = new ByteArrayInputStream(S.getBytes(encoding))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.