MIDP Socket problem (for experts)

J

jf

Hi.

I am doing an application (MIDP) that parses XML from a TCP connection.
I then submit the XML to the SAX Parser and then i parse it normally
using the events. All this ok if the socket receives the exactly
chuncks of that with well formed XML. But sometimes (if the broadband
is small) my socket reader cath's for example n bytes:

<p><xpto>sadasd<xpt

then it processes submit to the parser (it won't work because is not
well formed) and then i receive from the socket:

o>adasdsad</p>

i want to know if i can force the inputstream to be larger. Of course i
can read it byte byte, but then i must code a pre-parser that will
build the xml to the sax parser (or i code my own parser...pff)

Actually my socket reader is a thread running and wainting for data on
a inputstream. The run method is like this:


public void run() {



while(keepLooping)
{
try
{

int bytesavail = is.available();


if(bytesavail>0)
{
byte b[] = new byte[bytesavail];


int count = is.read(b,0,bytesavail);

if(count !=0)
{
String sResponse = new String(b,0,count,"ISO-8859-1");
listener.Read(sResponse);

}
}
else
try
{
sleep(1000);
}
catch(Exception e)
{
System.out.println("sleep error");
}

}
catch(Exception e)
{
System.out.println("Erro");
}

}


thanks

joao
 
O

Oliver Wong

Hi.

I am doing an application (MIDP) that parses XML from a TCP connection.
I then submit the XML to the SAX Parser and then i parse it normally
using the events. All this ok if the socket receives the exactly
chuncks of that with well formed XML. But sometimes (if the broadband
is small) my socket reader cath's for example n bytes:

<p><xpto>sadasd<xpt

then it processes submit to the parser (it won't work because is not
well formed) and then i receive from the socket:

o>adasdsad</p>

i want to know if i can force the inputstream to be larger. Of course i
can read it byte byte, but then i must code a pre-parser that will
build the xml to the sax parser (or i code my own parser...pff)

I'm not sure if I understand the last paragraph, but couldn't you break
up the string at points you know to be non-dangerous? Namely, don't break it
in the middle of a tag. When you see a '<' character, set the boolean
isItSafeToBreakHere = false. When you see '>', set the boolean to true.

You might be able to set isItSafeToBreakHere to true when you see a ' '
(the space character) as well. This protects you against tags with a large
number of attributes which might overflow your buffer. I don't know if your
parser is flexible enough to accept this though.

- Oliver
 
J

jf

Thanks a lot for answering.

The parser is SAX based. i think i will try to follow your idea and
make a pre-parser to build up the XML and then submit to the main
parser.

thanks

Joao
 
D

Darryl L. Pierce

I am doing an application (MIDP) that parses XML from a TCP connection.

Are you using something like kXML or are you writing your own parser?
I'd recommend using kXML and letting it read from your socket stream to
input the data if that's acceptable.
 
J

jf

i am using sax parser that comes with j2me (org.xml.sax). using another
parser, actually wouldn't solve the problem because XML was supplied
not well formed. Thanks anyway.
 
J

jf

Thanks for replying.

I tried your suggestion and it worked!

Thanks a lot and keep up the awesome work you doing with the Java
Glossary :)

Joao
 
?

=?iso-8859-1?q?Jo=E3o_Ferreira_(CSW)?=

The JAXP XML Parser Subset
The J2ME Web Services Reference Implementation provides three packages
that
make up the JAXP API subset. These are:
 javax.xml.parsers - contains the classes to obtain and reference a
platform's
given parser implementation.
 org.xml.sax - contains a subset of the SAX 2.0 API classes and
interfaces.
 org.xml.sax.helpers - contains a class for applications to extend and
receive
parse events.

OK JAXP... but the parser still is SAX model, i think.

Anyway, i keep having problems with this implementation. Now it's
blocking and of course i receive elements that i don't want. The
solution is to code the parser myself.....:(

thanks

joao
 
V

vishalsingh20

Hi,
The problem you are having is that the data arrives after the tag event
is fired because of latency. Two suggestions:
1. Code the parser yourself and provide breakpoints in the data.
2. Or use the StAX parser and pull the data yourself using the API.
That may give better control as to when to break.
http://jcp.org/en/jsr/detail?id=173.
 
D

Darryl L. Pierce

João Ferreira (CSW) said:
The JAXP XML Parser Subset

Okay, gotcha.

Anyway, i keep having problems with this implementation. Now it's
blocking and of course i receive elements that i don't want. The
solution is to code the parser myself.....:(

No, you don't. As I said previously, give kXML a try. I've used it and
it's a solid XML API for the MIDP. It provides DOM, SAX and also a pull
parser. Don't waste your time re-inventing the wheel...
 
?

=?iso-8859-1?q?Jo=E3o_Ferreira_(CSW)?=

ok i will try another parser..... thanks to all that helped me out.

Regards,

Joao
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top