我有一个 xmlStream,我正在使用 .xml 将其转换为 jsonStream org.apache.wink.json4j.utils.XML
。这是代码
public void process(InputStream xmlStream) {
final BufferedInputStream bufferedXmlStream = new BufferedInputStream(xmlStream);
PipedInputStream pipedJsonInputStream = new PipedInputStream();
final PipedOutputStream jsonStream = new PipedOutputStream(pipedJsonInputStream);
Thread xmlToJsonThread = new Thread(new Runnable() {
@Override
public void run() {
// put your code that writes data to the outputstream here.
try {
XML.toJson(bufferedXmlStream, jsonStream, true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
xmlToJsonThread.setDaemon(true);
xmlToJsonThread.start();
//process data from piped stream
BufferedReader reader = new BufferedReader(new InputStreamReader(
pipedJsonInputStream, StandardCharsets.UTF_8));
try {
// use reader to further process json in main thread...
parseJsonStream(reader);
} finally {
reader.close();
jsonStream.close();
}
}
当 XML.toJson 抛出异常时,我看到主线程没有退出。我该如何处理?你们认为这是将 XML 流转换为 Json 流以进行进一步处理的好方法吗?我真的很感激任何建议。非常感谢!