使用 Jsoup 从 html 字符串中仅提取 HTML 标签和属性
Extract only HTML tags and attributes from a html string using Jsoup
我只想获取 HTML 内容以及属性并删除文本。
输入字符串:
String html = "<p>An <br/><b></b> <a href='http://example.com/' target=\"h\"> <b> example <a><p></b>this is the </a> link </p>";
输出
<p><br></br><b></b><a href="http://example.com/" target="h"><b><a><p></p></a></b></a></p>
编辑:
google 或 Whosebug 中的大多数问题仅与删除 html 和提取文本有关。我花了大约 3 个小时来找到下面提到的解决方案。所以张贴在这里,因为它会帮助别人
希望这可以帮助像我这样希望仅从 HTML 字符串中删除文本内容的人。
输出
<p><br></br><b></b><a href="http://example.com/" target="h"><b><a><p></p></a></b></a></p>
String html = "<p>An <br/><b></b> <a href='http://example.com/' target=\"h\"> <b> example <a><p></b>this is the </a> link </p>";
Traverser traverser = new Traverser();
Document document = Jsoup.parse(html, "", Parser.xmlParser());// you can use the html parser as well. which will add the html tags
document.traverse(traverser);
System.out.println(traverser.extractHtmlBuilder.toString());
通过附加 node.attributes 将包括所有属性。
public static class Traverser implements NodeVisitor {
StringBuilder extractHtmlBuilder = new StringBuilder();
@Override
public void head(Node node, int depth) {
if (node instanceof Element && !(node instanceof Document)) {
extractHtmlBuilder.append("<").append(node.nodeName()).append(node.attributes()).append(">");
}
}
@Override
public void tail(Node node, int depth) {
if (node instanceof Element && !(node instanceof Document)) {
extractHtmlBuilder.append("</").append(node.nodeName()).append(">");
}
}
}
另一种解法:
Document document = Jsoup.parse(html, "", Parser.xmlParser());
for (Element element : document.select("*")) {
if (!element.ownText().isEmpty()) {
for (TextNode node : element.textNodes())
node.remove();
}
}
System.out.println(document.toString());
我只想获取 HTML 内容以及属性并删除文本。
输入字符串:
String html = "<p>An <br/><b></b> <a href='http://example.com/' target=\"h\"> <b> example <a><p></b>this is the </a> link </p>";
输出
<p><br></br><b></b><a href="http://example.com/" target="h"><b><a><p></p></a></b></a></p>
编辑: google 或 Whosebug 中的大多数问题仅与删除 html 和提取文本有关。我花了大约 3 个小时来找到下面提到的解决方案。所以张贴在这里,因为它会帮助别人
希望这可以帮助像我这样希望仅从 HTML 字符串中删除文本内容的人。
输出
<p><br></br><b></b><a href="http://example.com/" target="h"><b><a><p></p></a></b></a></p>
String html = "<p>An <br/><b></b> <a href='http://example.com/' target=\"h\"> <b> example <a><p></b>this is the </a> link </p>";
Traverser traverser = new Traverser();
Document document = Jsoup.parse(html, "", Parser.xmlParser());// you can use the html parser as well. which will add the html tags
document.traverse(traverser);
System.out.println(traverser.extractHtmlBuilder.toString());
通过附加 node.attributes 将包括所有属性。
public static class Traverser implements NodeVisitor {
StringBuilder extractHtmlBuilder = new StringBuilder();
@Override
public void head(Node node, int depth) {
if (node instanceof Element && !(node instanceof Document)) {
extractHtmlBuilder.append("<").append(node.nodeName()).append(node.attributes()).append(">");
}
}
@Override
public void tail(Node node, int depth) {
if (node instanceof Element && !(node instanceof Document)) {
extractHtmlBuilder.append("</").append(node.nodeName()).append(">");
}
}
}
另一种解法:
Document document = Jsoup.parse(html, "", Parser.xmlParser());
for (Element element : document.select("*")) {
if (!element.ownText().isEmpty()) {
for (TextNode node : element.textNodes())
node.remove();
}
}
System.out.println(document.toString());