1

How can I apply javascript to an html stored in string/stringbuffer ?
I am extracting the html of a webpage using Java

URL url = new URL("example.com");
InputStream is = url.openStream();
int ptr = 0;
StringBuffer buffer = new StringBuffer();
while ((ptr = is.read()) != -1) {
    buffer.append((char)ptr);
}
System.out.println(buffer);

and I want to apply javascript to the buffer to get innerHTML of some tag using document.getElementById().

My purpose is to get the innerHTML of some tag inside a webpage without opening it on browser. Am I using the correct way ? Is there some other way to do so ?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
iAmLearning
  • 1,153
  • 3
  • 15
  • 28
  • I've added the `java` tag to your question, because it sounds like you want to apply JavaScript within a Java program to the HTML stored in a string. (In any case, jQuery won't relate to this at all.) – T.J. Crowder Jan 05 '14 at 13:54

1 Answers1

2

You don't need JavaScript for this, not within a Java program.

You can use a DOM parser like Jsoup, and then use the methods in Jsoup that let you retrieve elements and their text. Jsoup isn't the only library that does this, you can find others if you search. It's one of the most popular at the moment.

Example using Jsoup:

Document doc = Jsoup.connect("http://example.com").get();
Element element = doc.getElementById("theIdValue");
// Read the text of the element:
String text = element.text();
// Or read the HTML of it
String html = element.html();
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875