You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Jsoup移除外层div标签与内部span标签?

Remove Unwanted Outer Divs and Quote Spans from HTML

The Problem

I need to strip out two outer <div>s (classes c44 c44v0 and c44w) plus the left/right quote <span>s (c44-lquote and c44-rquote) from my HTML to get the clean structure shown below. My current code gets rid of the target divs but replaces them with a new centered div—which isn't what I want. I'm looking for a cleaner, more precise solution.

Original HTML

<div class="c44 c44v0"> <div class="c44w"> <blockquote> <span class="c44-lquote"> <!-- # --> </span> <em>Being able to seamlessly integrate</em> with those providers via real-time data feeds. <span class="c44-rquote"> <!-- # --> </span> </blockquote> <div class="c44credit">Luke Kelly,<br /> Vice President of Finance, Carbon</div> </div> </div>

Desired Result

<blockquote> <em>Being able to seamlessly integrate</em> with those providers via real-time data feeds. </blockquote> <div class="c44credit">Luke Kelly,<br /> Vice President of Finance, Carbon</div>

My Attempt (Flawed)

doc.select("div.c44 c44v0 > div.c44w").forEach(div -> { div.parent().replaceWith(new Element(Tag.valueOf("div"), "").attr("align", "center").html(div.html())); });

What's Wrong with the Current Code?

Your code replaces the outer c44.c44v0 div with a brand new centered div—this adds unnecessary markup we don't need. Instead, we want to keep the inner content (the blockquote and credit div) directly, without wrapping them in another container.

The Fix (Using Jsoup)

Here's a straightforward approach that removes exactly the elements we don't want, leaving only the content we need:

// Parse your original HTML string
Document doc = Jsoup.parse(originalHtmlContent);

// Target the outermost div we want to replace
Element outerDiv = doc.selectFirst("div.c44.c44v0");
// Grab its direct child (the c44w div holding our content)
Element innerWrapper = outerDiv.selectFirst("div.c44w");

// Remove both quote spans from inside the blockquote
innerWrapper.select("span.c44-lquote, span.c44-rquote").remove();

// Replace the entire outer div with the cleaned inner content (no new wrapper!)
outerDiv.replaceWith(innerWrapper.children());

// Get the final cleaned HTML
String cleanedHtml = doc.body().html();

How This Works

  1. Target the right elements: We first grab the outermost c44.c44v0 div and its immediate child c44w which contains our actual content.
  2. Strip the quote spans: We select both left and right quote spans and remove them completely from the blockquote.
  3. Replace cleanly: Instead of wrapping the content in a new div, we replace the outer div directly with the cleaned child elements (the blockquote and credit div). This gives us exactly the structure you're after, no extra markup included.

内容的提问来源于stack exchange,提问作者Tusar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:15:40