giulio
Posts: 4
Nickname: giulio
Registered: Jun, 2002
|
|
Re: socket.getInputStream->xml Document
|
Posted: Jun 25, 2002 3:04 AM
|
|
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... :)
|
|