title: What's the recommended / fastest way to read xml from a POST to an http servlet?
body: <p>I've been trying to find a 'reference' example for the fastest / accepted way to read xml input from a http servlet, but can't seem to find a definitive answer.</p>
  
  <p>Here's the context:  we have an application, which is 12 years old, which works just fine in production, but I want to see if we're missing a trick and could make it faster.  </p>
  
  <p>It takes post requests consisting of xml (see the bottom of the post for schema and sample xml), and marshalls it to a java object using JAXB 1.0, then finds a request processor depending on request id, which then handles the request and writes the response.</p>
  
  <p>I'm a bit suspicious about the number of String operations, and I think maybe we should be using more buffered readers / writers, maybe a Scanner, and any other 'new' (ie more recent than java 1.2...) features.</p>
  
  <p>Here's a rough summary of how the requests are currently processed:</p>
  
  <pre><code>public void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
  try {
      InputStream is = request.getInputStream();
      InputStreamReader reader = new InputStreamReader( is ); 
      char[] buffer = new char[4096]; 
      StringWriter writer = new StringWriter(); 
      int bytes_read; 
      try
      {    //manual bufferering - can't we just use request.getReader() ?
          while ((bytes_read = reader.read(buffer)) != -1) { 
              writer.write(buffer, 0, bytes_read); 
          } 
      }
      catch(IOException e)
      {
          throw new RuntimeException("Unable to read the inputstream.  Please try again.");
      }
  
      String xml = writer.toString();
      //now we look for the payload and extract it:
      String tag = "payload";
      String header = null;
   String body = null;
   String innerPayload = null;
   String footer = null;
    int start = xml.indexOf("&lt;" + tag);
      if(start &lt; 0)
          throw new RuntimeException("Start tag \"" + tag + "\" was not found in the xml.");
  
      int end = xml.indexOf("&lt;/" + tag + "&gt;");
      if(end &lt; 0)
          throw new RuntimeException("End tag \"" + tag + "\" was not found in the xml.");
  
      int closeStart = xml.indexOf("&gt;", start);
      int closeEnd = xml.indexOf("&gt;", end);
  
      if(xml.charAt(closeStart - 1)=='/')
      {
          body = xml.substring(start,  closeStart + 1);
          innerPayload = null;
          header = xml.substring(0, closeStart + 1);
          footer = xml.substring(closeStart + 1, xml.length());
      }
      else
      {
          body = xml.substring(start, closeEnd + 1);
          innerPayload = xml.substring(closeStart + 1, end);
          header = xml.substring(0, closeStart + 1);
          footer = xml.substring(end, xml.length());
      }
  FsRequest envelope;     
  Object xml = JAXBRequestHelper.bind(header + footer);
  
          if(xml instanceof FsRequest)
              envelope = (FsRequest) xml;
          else
              throw new RuntimeException("Invalid XML request.");
  Object payloadType = JAXBRequestHelper.bind(innerPayload);
  //because the payload type is xs:any, I don't think we can avoid a cast here
  //in this case, it's a purchase:
  Purchase purchase = (Purchase) payloadType
  //request processor then handles the purchase using purchase.getMsisdn(), etc
  </code></pre>
  
  <p>A sample xml request might look like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
   &lt;fs-request id="100004"purchase_locale="en_GB"&gt;
    &lt;payload&gt;
     &lt;purchase&gt;
      &lt;msisdn&gt;13435456456&lt;/msisdn&gt;
      &lt;package-id&gt;package123&lt;/package-id&gt;
     &lt;/purchase&gt;
    &lt;/payload&gt;
  &lt;/fs-request&gt;
  </code></pre>
  
  <p>The definition of the xsd is a bit special, too. 'payload' is defined as xs:any which makes marshalling more tricky:</p>
  
  <pre><code> &lt;xs:element name="fs-request"&gt;
      &lt;xs:complexType&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="payload" type="common:payloadType" minOccurs="0"/&gt;    
          &lt;/xs:sequence&gt;
  
  
  &lt;xs:complexType name="payloadType"&gt;
      &lt;xs:sequence&gt;
          &lt;xs:any processContents="skip" minOccurs="0" /&gt;
      &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  </code></pre>
  
  <p>Is it just me or is this code a bit messy?  If so, is there an obvious way to make it cleaner / faster?  I'd love to see a reference example (although the xs:any payload type makes things a bit more complicated).</p>