我有 XML 数据作为必须解析的字符串,我使用以下代码将 XML 字符串转换为输入源:
StringReader reader1 = new StringReader( xmlstring);
InputSource inputSource1= new InputSource( reader );
我将输入源传递给
文档 doc = builder.build(inputSource);
我也想在另一个解析器类中使用相同的 inputSource1,但我正在关闭流。
我将如何处理这个问题,或者是否有其他方法将 XML 数据传递到文件以外的解析器?
最佳答案
查看 JavaDoc,似乎 InputSource
并非设计为由多个解析器共享和重用。
standard processing of both byte and character streams is to close them on as part of end-of-parse cleanup, so applications should not attempt to re-use such streams after they have been handed to a parser.
因此您必须创建一个新的InputSource
。如果您确实从 String
读取数据,那么 I/O 或内存成本不会有任何差异。
关于java - 使用流阅读器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2011165/