title: How to read XML returned by HTTP POST request?
body: <p><strong>NOT a duplicate of my other question.</strong></p>
  
  <p>I am sending a <code>POST</code> request like this:</p>
  
  <pre><code>        String urlParameters = "a=b&amp;c=d";
          String request = "http://www.example.com/";
  
          URL url = new URL(request);
          HttpURLConnection connection = (HttpURLConnection) url.openConnection();      
          connection.setDoOutput(true);
          connection.setDoInput(true);
          connection.setInstanceFollowRedirects(false); 
          connection.setRequestMethod("POST");
          connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
          connection.setRequestProperty("charset", "utf-8");
          connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
          connection.setUseCaches(false);
  
          DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
          wr.writeBytes(urlParameters);
          wr.flush();
          wr.close();
          connection.disconnect();
  </code></pre>
  
  <p>How can I read the xml response returned from a HTTP <code>POST</code> request? Particularly, I want to save the response file as a .xml file, and then read it. For my usual <code>GET</code> requests, I use this:</p>
  
  <pre><code>    SAXBuilder builder = new SAXBuilder();
      URL website = new URL(urlToParse);
      ReadableByteChannel rbc = Channels.newChannel(website.openStream());
      FileOutputStream fos = new FileOutputStream("request.xml");
      fos.getChannel().transferFrom(rbc, 0, 1 &lt;&lt; 24);
      fos.close();
      // Do the work
  </code></pre>
  
  <p><strong>Addendum</strong>: I'm using the following code and it works just fine. However, it neglects any spacing and new lines and treats the complete XML contents as a single line. How do I fix it?</p>
  
  <pre><code>    InputStream is = connection.getInputStream();
      BufferedReader br = new BufferedReader(new InputStreamReader(is));
      StringBuilder sb1 = new StringBuilder();
      String line;
      while ((line = br.readLine()) != null) {
          sb1.append(line);
      }
      FileOutputStream f = new FileOutputStream("request.xml");
      f.write(sb1.toString().getBytes());
      f.close();
      br.close();
  </code></pre>