以下是我的代码,用于解析HTML并将<small>
标记的出现替换为<span>
的
String html =
"<html>"+
"<body bgcolor=''>"+
"<table class='tabletext'>" +
"<tr align='center' style='background:#FFFFFF'>" +
"<td class='classa'><span id='fd'><span>10</span></span></td>" +
"<td>10.00</td>" +
"<td><small>£0.00</small></td>" +
"<td>£280.00</td>" +
"<td>" +
"<ul>"+
"<li><a href=''>ok</a></li>"+
"</ul>"+
"<a href='/cart.php?action=add&qty=10&id=2628' title='Click here to add this item to your cart'>" +
"<img alt='Click here to add this item to your cart' src='/images/addtocart.gif' border='0' />" +
"</a>" +
"</td>" +
"</tr>" +
"<tr><td><span>Hello2</span></td></tr>"+
"</table>"+
"</body>"+
"</html>";
Document doc = Jsoup.parse(html);
Elements elements = doc.select("small");
Element element2 = elements.get(0);
System.out.println(element2.replaceWith("<span>"));
但是上面的代码工作不正常。
答案 0 :(得分:3)
如果您要将所有<small>
代码替换为<span>
,则应正确使用Node.replaceWith
。
Document doc = Jsoup.parse(html);
for (Element small: doc.select("small")) {
small.replaceWith(new Element(Tag.valueOf("span"), "").text(small.text()));
}
System.out.println(doc.html()); // prints html with <small> replaced by <span>
答案 1 :(得分:3)
另一种解决方案是重命名标记:
Document doc = Jsoup.parse(html);
doc.select("small").tagName("span");
答案 2 :(得分:0)
它适用于所有标签:
Document html=Jsoup.parse(htmlString);
html.select("small").tagName("span");