Is there a way I can parse an Xml Document from a Socket InputStream without closing the stream on the client side? I only have control the the Server side receiving the Xml and the socket will remain open since the server will be sending a response back to the client.
Can I tell it to stop and return the Document when it finds the root element closing tag, I'd need to modify the parser wouldn't I? Why would it even bother to parse further since having multiple root elements in a Document would make it not well-formed? It keeps parsing after the end element because it's checking for trailing comments or processing instructions, which I do not care about in my case and would ignore them.
The Xml I send is well-formed and is properly parsed from a FileInputStream, since it has a clear EOF, but hangs when being parsed from a Socket InputStream that does not close.
The client does not close the stream after sending the Xml because they expect a response over the socket.
Here is my code:
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
}
Here is the stack trace of where it is hanging:
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
Thanks for any suggestions.