title: How to save xml data from a URL to a file?
body: <p>What I wanna do is get the content of this URL :</p>
  
  <p><a href="https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&amp;requestType=retrieve&amp;format=xml&amp;stationString=CYQB&amp;hoursBeforeNow=2" rel="nofollow">https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&amp;requestType=retrieve&amp;format=xml&amp;stationString=CYQB&amp;hoursBeforeNow=2</a></p>
  
  <p>and copy it to a file so I can parse it and use the elements.</p>
  
  <p>Here is what I have so far :</p>
  
  <pre><code>package test;
  
  import java.io.*;
  import java.net.*;
  
  import org.apache.commons.io.FileUtils;
  
  public class JavaGetUrl {
  
  @SuppressWarnings("deprecation")
  public static void main(String[] args) throws FileNotFoundException {
  
      URL u;
      InputStream is = null;
      DataInputStream dis;
      String s = null;
  
      try {
  
          u = new URL(
                  "https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&amp;requestType=retrieve&amp;format=xml&amp;stationString=CYQB&amp;hoursBeforeNow=2");
  
          is = u.openStream(); // throws an IOException
  
          dis = new DataInputStream(new BufferedInputStream(is));
  
          while ((s = dis.readLine()) != null) {
              System.out.println(s);
              FileUtils.writeStringToFile(new File("input.txt"), s);
          }
  
      } catch (MalformedURLException mue) {
  
          System.out.println("Ouch - a MalformedURLException happened.");
          mue.printStackTrace();
          System.exit(1);
  
      } catch (IOException ioe) {
  
          System.out.println("Oops- an IOException happened.");
          ioe.printStackTrace();
          System.exit(1);
  
      } finally {
  
          try {
              is.close();
          } catch (IOException ioe) {
  
          }
  
      }
  
  }
  }
  </code></pre>
  
  <p>The problem is that the content of s does not show up in input.txt.</p>
  
  <p>If I replace s by any other strings it works. So I guess it's a problem with the data of s. Is it because it's xml?</p>
  
  <p>Thank you all for the help.</p>