title: How can I improve how I read XML from Socket
body: <p>I am working on a Java client that connects to a server via socket and sends/receives XML messages. I know what the schema of the messages will be. I have it working currently, but it feels messy and I haven't tested it yet to see how it handles multiple XMLs sent from the server at once. </p>
  
  <p>Here is the code:</p>
  
  <pre><code>  private static BufferedReader socketIn;
    private static PrintWriter socketOut;
    private static final String SERVICES_FILE = "/etc/services";
  
    public static void main(String[] args) {
      int port = getServicePortByName("Service1", "tcp");
      try {
        logger.info("Creating socket port with ip: " + InetAddress.getLocalHost() + " and port: " + port);
        Socket socket = new Socket(InetAddress.getLocalHost(), port);
        socketOut = new PrintWriter(socket.getOutputStream(), true);
  
        String message = createMessageXML("LOGIN");
        socketOut.println(message);
  
        socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        StringBuilder inputLine = new StringBuilder();
        String tmp;
  
        while (true) {
        //loop to keep listening/parsing
          while(socketIn.ready()) {
            tmp = socketIn.readLine();
            inputLine.append(tmp);
          }
          if(inputLine.length() &gt; 0) {
            parseReceivedMessage(inputLine.toString());
          }
          inputLine.replace(0, inputLine.length(), "");
        }
  
      } catch (UnknownHostException ex) {
        Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IOException ex) {
        Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
  </code></pre>
  
  <p>the parseReceivedMessage method just takes in a String and converts it to a Document object. My question is, is there a better or cleaner way to do this instead of using a StringBuilder? I originally wanted to pass an InputStream to the DOM builder parse method.
  I saw some examples making use of ByteArray streams, but I had some issues with those. Let me know if anything needs clarification.</p>