1

I am using URLConnection in Java to get the response of the webpage. I dont want to use JSoup.

When using the below code, I am getting the whole response as a single line like below.

<html><head><stylesheet>something,something,something,</stylesheet><script>something,something,something,something</script></head><body>something,something,something,something,something </body></html>

But I want to get them like below

<html>
<head>
<stylesheet>
    something,
    something,
    something
</stylesheet>
<script>
    something,  
    something, 
    something, 
    something
</script>
</head>
<body>
    something,
    something,
    something,
    something,
    something
</body>
</html>

Here is the code I am using,

HttpURLConnection connection = (HttpURLConnection) url.openConnection();        

connection.setRequestMethod("GET");
connection.connect();           
InputStream content = (InputStream) connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;

while ((line = in.readLine()) != null) {
    String s = line.toString();
    System.out.println(s);
}
  • 6
    Please have a look [here](http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java). Your question is similar to asking for ways to "pretty" format XML. – Tim Biegeleisen Jan 20 '16 at 04:38
  • 1
    `hi\nhi\nhi\nhi\nhi\nhi\nhi\nhi\nhi\nhi\n` – Matt Clark Jan 20 '16 at 04:39
  • 1
    Are you getting that from the console? In the case of JSON, if I copy and paste it on a word file or something, it usually makes the json pretty. – bmarkham Jan 20 '16 at 04:40
  • JSoup is probably the best option (why do you not want to use it ?), since most of the answers to the question that Tim linked to won't work well with invalid HTML (which is quite common). In fact, Jsoup may be faster than the standard Java XML parsing tools. – Jonas Czech Jan 20 '16 at 12:07

0 Answers0