I have a String with HTML formated text (not a whole webpage).
How can I get all the HTML content after a particular tag using Jsoup?
To be more concret. Assuming I have the following string:
String input = "<div>a</div><p>b</p><strong>c</strong>";
I would like to get:
String output = "<p>b</p><strong>c</strong>";
Hence I am doing
Document doc = Jsoup.parseBodyFragment(input); // parse
Element p = doc.select("p"); // select p
And I have a hard time firguring out how to output what after p. Let assume for simplicity that p is unique.
Another input/output (as asked):
String input = "<br /><strong>a</strong><strong>b</strong><p>c</p><div>d</div><br />";
String output = "<p>c</p><div>d</div><br />";
Thank you in advance.
Here's some code - hope it helps you a bit:
String input = "<div>a</div><p>b</p><strong>c</strong>";
Document doc = Jsoup.parse(input);
Elements elements = doc.select("p ~ *");
Elements group = new Elements();
group.add(elements.first().previousElementSibling());
for( Element element : elements )
{
group.add(element);
}
// You can work with 'group' too
String output = group.toString();
Output:
example 1:
<p>b</p>
<strong>c</strong>
example 2:
<p>c</p>
<div>
d
</div>
<br />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.