0

how can i get an xml file from a url to a file on my desktop using java 8? i tried using the BufferedReader and StringBuilder which downloads the file on the console,

is there another way of doing it so that I get the xml file on my desktop and extract the information that I need ? my code:

                URL url1 = new URL("http://api.eancdn.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=28&apiKey=m9sur8fsbdemjck7y9yydmfx&locale=en_EN&currencyCode=USD&latitude=42.50631740000001&longitude=1.5218355");

                InputStream is1 = url1.openStream();
                BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
                StringBuilder sb = new StringBuilder();
                String line;
                while((line=br1.readLine())!=null){
                    sb.append(line +"\n");
                }
                System.out.println(sb.toString());
Kkk
  • 1
  • 2
  • 1
    Can you show us what you tried so far? – Markus W Mahlberg Nov 01 '14 at 14:55
  • save your string to a file instead of printing it to the console. also, using Readers and Strings when dealing with xml is a great way to corrupt the data. treat it as bytes and use the xml utilities in java to interpret the data. – jtahlborn Nov 01 '14 at 15:03
  • @MarkusWMahlberg just posted what i tried. – Kkk Nov 01 '14 at 15:57
  • @jtahlborn what xml utility can interpret the data ? if you can write the code it would be great ! – Kkk Nov 01 '14 at 16:11

1 Answers1

0

In this way you can use java XML Sax parser

URL url = new URL("http://www.example.com/example.xml");
InputStream stream = url.openStream();
Document doc = docBuilder.parse(stream);
njjnex
  • 1,490
  • 13
  • 23
  • isnt there another way of doing it without having to download the JAXP api ? – Kkk Nov 01 '14 at 16:13
  • download it as regular file using nio http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java – njjnex Nov 01 '14 at 18:10