Code example:
<body>
<p>123124</p>
<p>214565</p>
<p>123333</p>
<img src = "aboba"/>
<p>faaaa</p>
<p>aaaaa</p>
<a href="src"></a>
<p>1111</p>
</body>
Using jsoup to wrap in <div class="test"></div>
I expect that it will turn out something like that
Result:
<div class="test">
<p>123124</p>
<p>214565</p>
<p>123333</p>
</div>
<img src = "aboba"/>
<div class="test">
<p>faaaa</p>
<p>aaaaa</p>
</div>
<a href="src"></a>
<div class="test">
<p>1111</p>
</div>
UPDATE: correction of the expected result
2
Answers
To find identical HTML tag elements that are not separated by other tags using Jsoup, you can follow these steps:
select
method to find the specific tag elements you’re interested in.Here’s a code example in Java that demonstrates this process:
The HTML contains a
<div>
with multiple<p>
elements. The code iterates through the<p>
elements and checks if they are siblings (adjacent) to each other. If they are, it prints out the identical adjacent elements.You can adjust the
tagName
variable to the specific tag name you’re interested in (e.g.,"p"
for<p>
tags).You can use a ListIterator to mutate an Elements collection.
Additionally, you can utilize the next and previous methods to adjust the iterator’s cursor.
Here, the element and next variables hold the current and following elements, respectively.
The concept is to add all p elements to the new div element.
The div is only added when the next element is null or not a p element.
Here is the output for body.