So yeah, suppose I have this piece of HTML
<p>And finally, how about some <a href="http://www.yahoo.com/">Links?</a></p>
and I want to access and modify the "And finally, how about some" part only, and get this:
<p>new text <a href="http://www.yahoo.com/">Links?</a></p>
I can't seem to figure out how. Here's what I've tried so far:
Document doc = null;
try {
doc = Jsoup.connect("http://csb.stanford.edu/class/public/pages/sykes_webdesign/05_simple.html").userAgent("Mozilla").get();
} catch (IOException e1) {
e1.printStackTrace();
}
Elements d = doc.body().children();
Element e = d.get(20); //Assuming the HTML line in question is found at index 20
e.text("new text") //just outputs <p>new value</p>, which is not good for me
It seems that I can access it by
Element e = d.get(20);
System.out.println("\n"+e.ownText()); //outputs: And finally, how about some
but modifying it doesn't work.
Element e = d.get(20);
String s = e.toString().replace(e.ownText(), "new text");
e.text(s);
System.out.println(e.toString());
The output for the code above is
<p><p>changed <a href="http://www.yahoo.com/">Links?</a></p></p>
It seems to be taking the tags as literals, but I want them as < or > because I then have to re build the webpage with the new text.
Any kind of help will be hugely appreciated.