XML & Streams

admin

Administrator
Staff member
I have an applet which in its run() method, sends a request to a server every5 seconds. The server sends the applet back a stream which I then parse usingSAX. I first had a problem with reaching endDocument(). I worked around thisby looking for the last element in the xml stream & throwing an exception.This works, but my stream somehow gets closed, and I cannot reuse it forparsing.//Part of Applet Codeprivate URLConnection m_conn = null;private PrintWriter m_out = null;public void init(){URL server = new URL("http://localhost:80");m_conn = server.openConnection();m_conn.setDoOutput(true);m_conn.setDoInput(true);m_conn.setUseCaches(false);m_out = new PrintWriter(m_conn.getOutputStream());}public void run(){long interval = 5000; //5 secondswhile (Thread.currentThread() == m_thread){updateState();m_frame.repaint();try{Thread.sleep(interval);}catch(InterruptedException e){break;}}}private void updateState(){//Send request to serverm_out.println(m_query);m_out.flush();//Get response from serverInputStream stream = m_conn.getInputStream();m_frame.initializeState(stream); //Method that parses}//Part of server codepublic class ThreadedHandler extends Thread{private Socket m_socket = null;private CallCenterServer m_server = null;private BufferedReader m_in = null;private PrintWriter m_out = null;public ThreadedHandler(Socket socket, CallCenterServerserver){m_socket = socket;m_server = server;try{m_in = new BufferedReader(new InputStreamReader(newBufferedInputStream(m_socket.getInputStream(),1000)));m_out = new PrintWriter(m_socket.getOutputStream(),true);}catch(IOException e){System.out.println(e);}}public void writeMessage(String msg){//Sends message to applettry{FileInputStream stream = new FileInputStream("Z:/Lynn/ibd/data" + m_tempFileCount + ".xml");BufferedReader bReader = new BufferedReader(newInputStreamReader(stream));m_out = new PrintWriter(m_socket.getOutputStream(),true);String line = null;while((line = bReader.readLine()) != null){m_out.println(line);}m_out.flush();bReader.close();stream.close();//Increment to get next file (can be 1 - 6)if (m_tempFileCount < 6){m_tempFileCount++;}}catch(FileNotFoundException e){}catch(IOException e){}}}//Parsing codepublic DeskStateParser(InputStream stream){try{InputStreamReader reader = new InputStreamReader(stream);m_input = new InputSource(reader);m_parser = ParserFactory.makeParser();m_parser.setDocumentHandler(new MyDocHandler());m_parser.setErrorHandler(new MyErrorHandler());}....Then I call:m_parser.parse(m_input);It goes through the xml file once - I reach the last tag.This method never gets called:public void endDocument() throws SAXExceptionI'm guessing it doesn't happen because the stream is not closed.When I had done testing passing a file to the InputSource, it reached theendDocument method, and all was well. But, I need to use a stream instead.Someone suggested throwing a SAXException when you reach your last xml tag.I tried this - it threw the exception, but then this line in my applet failed:InputStream stream = m_conn.getInputStream();because it said the InputStream was closed.Any suggestions? Does anyone know of any samples online that use streamsinstead of files for the InputSource?Thanks,Lynn
 
Back
Top