I want to remove everything between a tag. An example input may be
Input:
start
delete from below
-
You better iterate over all elements found. so you can be shure that
- a.) all elements are removed and
- b.) there's nothing done if there's no element.
Example:
Document doc = ...
for( Element element : doc.select("div.XYZ") )
{
element.remove();
}
Edit:
( An addition to my comment )
Don't use exception handling when a simple null- / range check is enough here:
doc.select("div.XYZ").first().remove();
instead:
Elements divs = doc.select("div.XYZ");
if( !divs.isEmpty() )
{
/*
* Here it's safe to call 'first()' since there at least one element.
*/
}