The Artima Developer Community
Sponsored Link

Java Answers Forum
socket.getInputStream->xml Document

2 replies on 1 page. Most recent reply: Jun 25, 2002 3:04 AM by giulio

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
giulio

Posts: 4
Nickname: giulio
Registered: Jun, 2002

socket.getInputStream->xml Document Posted: Jun 24, 2002 2:55 AM
Reply to this message Reply
Advertisement
hello everybody,
i found great problems trying to convert the getInputStream from a socket to an xml document , that will be parsed by sax.
saxBuilder needs a ByteArrayInputStream object while what i get from my socket is a weird Byte array that i can't control and neither cast to a ByteArrayInputStream.
thanks in advance if someone knows how to treat/convert raw data from a socket.
ciao


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: socket.getInputStream->xml Document Posted: Jun 24, 2002 10:21 AM
Reply to this message Reply
getInputStream() returns an Inputstream, so I don't understand why you would instead get an array of Byte from the Socket (unless you are constructing it yourself or using some derivation of Socket which gives it to you)...

Maybe you can post a snippet of code to illustrate your problem.

giulio

Posts: 4
Nickname: giulio
Registered: Jun, 2002

Re: socket.getInputStream->xml Document Posted: Jun 25, 2002 3:04 AM
Reply to this message Reply
in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));


public void run() {
try {
byte[] mybytesarray = new byte[2048];
int contatore=0;
char caratteresingolo[] = new char[1];
while(in.read(caratteresingolo,0,1) != -1) {
StringBuffer stringBuffer = new StringBuffer(8192);
while(caratteresingolo[0] != '\0') {
System.out.println("caratteresingolo["+contatore+"]="+caratteresingolo[0]);
//System.arraycopy(caratteresingolo,0,mybytesarray,contatore,contatore);
stringBuffer.append(caratteresingolo[0]);
in.read(caratteresingolo, 0 ,1);
contatore++;
}


// GESTIONE dell XML RICEVUTO

SAXBuilder saxb = new SAXBuilder(false);
Document xmlDoc;
int count=stringBuffer.length();

System.out.println ("count="+count);
messaggio=stringBuffer.toString();
System.out.println("messaggio per il server "+messaggio);





try
{
// xmlDoc = saxb.build(messaggio); --> can't do that. build() wants a file as an argument
// or a ByteArrayInputStream
ByteArrayInputStream ilmiostream = new ByteArrayInputStream(mybytesarray);
xmlDoc = saxb.build( ilmiostream); // but how to do the conversion?
Element el = xmlDoc.getRootElement();
String name = el.getName();
System.out.println (name);

}
catch (JDOMException ex)
{
System.out.println ("trovata JDOM XCeption "+ex);

}



}
} catch(IOException ioe) {

} finally {
eliminaClient();

}
}

ok!
here's my code: this is the run method of a thread that has the responsability to parse xml messages from a socket, unluckily the cliento connected to the socket ends all of his xml messages with a '\0' character, so i have to look for it before parsing the xml. anyway my main problem is to convert this input stream to something like a byteArrayInputStream, that seems the only valid argument i can submit to saxBuilder to parse a document. i've tried to copy the content of the input stream with the system.arraycopy() function but it doesn't seem to work throwing lotta arraystoreexceptions. if you can help i'd be really thankful... :)

Flat View: This topic has 2 replies on 1 page
Topic: Formating in Java Previous Topic   Next Topic Topic: draw

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use