Java Program to Extract Content from a HTML document
Last Updated :
19 Nov, 2022
HTML is the core of the web, all the pages you see on the internet are HTML, whether they are dynamically generated by JavaScript, JSP, PHP, ASP, or any other web technology. Your browser actually parses HTML and render it for you But if we need to parse an HTML document and find some elements, tags, attributes or check if a particular element exists or not. In java, we can extract the HTML content and can parse the HTML Document.
Approaches:
- Using FileReader
- Using the Url.openStream()
Approach 1: The library called the FileReader which provides the way to read any File irrespective of any Extension. The way to append the HTML lines to the String Builder is as follows:
- Using the FileReader to read the file from the Source Folder and further
- Append each line to the String builder.
- When there is not any content left in HTML Document then close the open File using the function br.close().
- Print out the String.
Implementation:
Java
import java.io.*;
public class GFG {
public static void main(String[] args)
throws FileNotFoundException
{
StringBuilder html = new StringBuilder();
FileReader fr = new FileReader(
"C:\\Users\\rohit\\OneDrive\\Desktop\\article.html" );
try {
BufferedReader br = new BufferedReader(fr);
String val;
while ((val = br.readLine()) != null ) {
html.append(val);
}
String result = html.toString();
System.out.println(result);
br.close();
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
|
Output:
Approach 2: Using the Url.openStream()
- Call the url.openStream() function that initiates the new TCP connection to the Server that the URL provides it to.
- Now, HTTP gets Request is sent to the connection after the server sends back the HTTP response containing the information into it.
- That information is in the form of the bytes then that information is read using the InputStreamReader() and openStream() method return the data to the program.
BufferedReader br = new BufferedReader(new InputStreamReader(URL.openStream()));
- First, we open the URL using the openStream() to fetch the information. The information is contained in the URL in the form of bytes if the connection is all OK (means is shows 200) then HTTP request to the URL To fetch the content.
- Then the information is collected in the form of bytes using the inputStreamReader()
- Now the loop is run to print the information as the demand is to print the information in the console.
while ((val = br.readLine()) != null) // condition
{
System.out.println(val); // execution if condition is true
}
Implementation:
Java
import java.io.*;
import java.util.*;
import java.net.URL;
public class GFG {
public static void main(String[] args)
throws FileNotFoundException
{
try {
String val;
URL URL = new URL(
BufferedReader br = new BufferedReader(
new InputStreamReader(URL.openStream()));
while ((val = br.readLine()) != null ) {
System.out.println(val);
}
br.close();
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...