Java 如何从字符串创建 org.xml.sax.InputSource?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4280761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create an org.xml.sax.InputSource from a String?
提问by Chiggins
I am following a guide, and it gives me the following code:
我正在遵循指南,它给了我以下代码:
InputSource inputSource = new InputSource(new FileInputStream(new File("/path/to/xml/file.xml"))));
What I would like to know, is how I can still create an org.xml.sax.InputSource
, but instead of reading the content of a file, use a String
variable that I already have.
我想知道的是,我仍然可以创建一个org.xml.sax.InputSource
,而不是读取文件的内容,而是使用String
我已经拥有的变量。
采纳答案by Gavin H
Use a StringReader
instead of a FileInputStream
.
使用 aStringReader
代替 a FileInputStream
。
See the documentation for StringReader
请参阅StringReader的文档
example:
例子:
InputSource inputSource = new InputSource( new StringReader( myString ) );
回答by khachik
InputSource inputSource = new InputSource(new java.io.StringReader(string));
if it is org.xml.sax.InputSource
.
InputSource inputSource = new InputSource(new java.io.StringReader(string));
如果是org.xml.sax.InputSource
。