We help IT Professionals succeed at work.

Parsing XML From a Socket InputStream using Xerces 1.4.4

Celedor
Celedor asked
on
Medium Priority
1,642 Views
Last Modified: 2013-11-23
I'm trying to figure out the problem with using Xerces 1.4.4 to parse XML data that has been passed through a socket...  I have no idea whats going wrong.  Here's the code used:

InputStream in = socket.getInputStream();

InputStreamReader contentReader = new InputStreamReader(content);

InputSource inputsource;

DOMParser domparser = new DOMParser();
domparser.setIncludeIgnorableWhitespace(false);

inputsource = new InputSource(contentReader);

domparser.parse(inputsource);
//Process the DOM Here


The following code works if I manually take the data from the InputStream and turn it to a String, then pass a StringReader over
to the DOMParser.

I also tried using a FileInputStream (to experiment reading from an XML File) instead of using a Socket InputStream by
replacing:

"InputStream in = socket.getInputStream();"

with

"FileInputStream in = new FileInputStream("testXML.xml");"

and the parser works...

So where could the problem possibly lie?

Thanks in advance!
Comment
Watch Question

Do you get an exception? Try to wrap the inputstream from the socket in a buffer:
InputStream in = new BufferedInputStream(socket.getInputStream());
CERTIFIED EXPERT
Top Expert 2016

Commented:
What happens when you try

domparser.parse(new InputSource(new InputStreamReader(socket.getInputStream())));

?
Hi,

I think while the data is travelling through sockets, for data integrity purposes, it is converted to bytes, and which has to explicitly casted to string, in case, a user wants to view it.

Regards,

Warturtle

Author

Commented:
No exception is being thrown, the parser just hangs (it looks like its waiting for some way to signal the end of the stream, perhaps?).

CEHJ: I'd expect the same thing to happen... But I will give it a try, just to be sure. Thanks!

Reviewing the code I posted up there,

"InputStreamReader contentReader = new InputStreamReader(content);"

uses content because its in a different method that accepts an InputStream - but this should be equivalent to:

InputStreamReader contentReader = new InputStreamReader(in);
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>
No exception is being thrown, the parser just hangs (it looks like its waiting for some way to signal the end of the stream, perhaps?).
>>

It's probably either

a. blocking waiting to read bytes that are not yet available on
b. waiting for the sender to close the stream after sending

Author

Commented:
Hmm.. That helps a lot...

That means I can probably test for B by building a lightweight socket sender myself.

How would you test for A?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>How would you test for A?

Just do a test print to System.out. If you're getting any data, it's probably B

Author

Commented:
Thanks a lot!  I will get back to you tomorrow on the results.

Author

Commented:
It seems to be waiting for the entire socket to close, rather than simply the outputstream.  Once the socket is closed, the Parser then succesfully parses.  But this closes the connection, so no reply can be sent.

I looked at the Java Tutorial client/server implementation, and it seems that the server/client for some sort of "signal" that would signify that the other party has stopped talking, so they could maintain the connection while "conversing".

This being the case, I guess you really have to read it into a String (while having to establish some sort of protocol to signify end of input)?  Or is there some other way to fix this?

Thanks again...
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
There's a way to handle this probably - do i take it that you know the incoming message format Celedor?
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Right.  That about solves the problem.

CEHJ, that's very similar to how I did it.  But now, thanks to you, I have a much better understanding of the problem.

orangehead911, that's an interesting pattern which could prove useful to know.

You've both been very very helpful... Thanks a lot! :-)
CERTIFIED EXPERT
Top Expert 2016

Commented:
8-)
My pleasure! :-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.