有没有一种方法可以在不关闭客户端流的情况下从 Socket InputStream 解析 Xml 文档?我只能控制接收 Xml 的服务器端,并且套接字将保持打开状态,因为服务器将向客户端发送响应。当它找到根元素结束标记时,我可以告诉它停止并返回 Document,我需要修改解析器,不是吗?它在结束元素之后继续解析,因为它正在检查尾随注释或处理指令,在我的情况下我不关心并且会忽略它们。在XML我送的良好和正确地从一个FileInputStream解析,因为它有一个明确的EOF,但是,当从一个套接字的InputStream被解析挂起这不会关闭。客户端在发送 Xml 后不会关闭流,因为它们期望通过套接字获得响应。这是我的代码:try ( ServerSocket server = new ServerSocket(port); Socket sock = server.accept(); InputStream in = sock.getInputStream(); ) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); db.setErrorHandler(MyErrorHandler); db.setEntityResolver(MyEntityResolver); // below hangs, waiting for stream to close I think Document doc = db.parse(in); // .. process document // .. send response} 这是它挂在哪里的堆栈跟踪:SocketInputStream.socketRead0(FileDescriptor, byte[], int, int, int) line: not available [native method] SocketInputStream.socketRead(FileDescriptor, byte[], int, int, int) line: 116 SocketInputStream.read(byte[], int, int, int) line: 171 SocketInputStream.read(byte[], int, int) line: 141 XMLEntityManager$RewindableInputStream.read(byte[], int, int) line: 2919 UTF8Reader.read(char[], int, int) line: 302 XMLEntityScanner.load(int, boolean, boolean) line: 1895 XMLEntityScanner.skipSpaces() line: 1685 XMLDocumentScannerImpl$TrailingMiscDriver.next() line: 1371 XMLDocumentScannerImpl.next() line: 602 XMLDocumentScannerImpl(XMLDocumentFragmentScannerImpl).scanDocument(boolean) line: 505 XIncludeAwareParserConfiguration(XML11Configuration).parse(boolean) line: 841 XIncludeAwareParserConfiguration(XML11Configuration).parse(XMLInputSource) line: 770 DOMParser(XMLParser).parse(XMLInputSource) line: 141 DOMParser.parse(InputSource) line: 243 DocumentBuilderImpl.parse(InputSource) line: 339 DocumentBuilderImpl(DocumentBuilder).parse(InputStream) line: 121 感谢您的任何建议。
添加回答
举报
0/150
提交
取消