如何从String创建org.xml.sax.InputSource?

Chi*_*ins 46 java xml

我正在关注指南,它给了我以下代码:

InputSource inputSource = new InputSource(new FileInputStream(new File("/path/to/xml/file.xml"))));
Run Code Online (Sandbox Code Playgroud)

我想知道的是,我仍然可以创建一个org.xml.sax.InputSource,但不是阅读文件的内容,而是使用String我已经拥有的变量.

Gav*_*n H 83

使用StringReader而不是a FileInputStream.

请参阅StringReader的文档

例:

InputSource inputSource = new InputSource( new StringReader( myString ) );
Run Code Online (Sandbox Code Playgroud)