Solved

Parsing XML From a Socket InputStream using Xerces 1.4.4

Posted on 2004-04-04
15
1,567 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!
0
Comment
Question by:Celedor
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10751230
Do you get an exception? Try to wrap the inputstream from the socket in a buffer:
InputStream in = new BufferedInputStream(socket.getInputStream());
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10751495
What happens when you try

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

?
0
 
LVL 16

Expert Comment

by:warturtle
ID: 10752069
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
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:Celedor
ID: 10752097
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);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10752121
>>
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
0
 

Author Comment

by:Celedor
ID: 10752132
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10752163
>>How would you test for A?

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

Author Comment

by:Celedor
ID: 10752168
Thanks a lot!  I will get back to you tomorrow on the results.
0
 

Author Comment

by:Celedor
ID: 10755230
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...
0
 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 40 total points
ID: 10755282
You could use the control/data connection pattern to solve the problem. It has been used for many years in e.g. ftp. You create one socket for control, i.e. sending commands and receiving replies, and one socket is created for data when needed. Once the data has been sent over the data socket, the socket is closed. This particular part of the pattern should solve your problem.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10756575
There's a way to handle this probably - do i take it that you know the incoming message format Celedor?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 80 total points
ID: 10756630
This is what i was going to suggest:

String line = null;
StringBuffer buffer = new StringBuffer();
while ((line = in.readLine()) != null) {  // 'in' is a BufferedReader on socket input stream
      buffer.append(line);
      if (line.indexOf(CLOSE_TAG) > -1) { // message close tag found
            processXml(buffer); // send buffer for processing
            // [maybe in.close()]
                          break;
      }
}
0
 

Author Comment

by:Celedor
ID: 10756968
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! :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10757277
8-)
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10757409
My pleasure! :-)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javap not working 8 44
eclipse apache tomcat admin console 52 96
SHA2 certs for IIS AND Java? 2 81
Configure a Bean in an XML file 4 31
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question