Date: 2016mar8
Language: Java
Library: jsoup
Q. Using jsoup how can I get the outerHtml() without the innerHtml() ?
A.
// outerHtml() minus innerHtml()
String startHtml(Element e) {
if (e == null) return "";
StringBuilder out = new StringBuilder("<" + e.tagName());
Attributes att = e.attributes();
if (att != null) {
String atts = att.toString();
if (atts.length() > 0) {
out.append(" " + atts);
}
}
out.append(">");
return out.toString();
}
String endHtml(Element e) {
if (e == null) return "";
return "</" + e.tagName() + ">";
}
For example, if you have:
<div att1="stuff" att2="thing">
<span>The inside</span>
</div>
startHtml() will give just:
<div att1="stuff" att2="thing">
outerHtml() gives it all:
<div att1="stuff" att2="thing">
<span>The inside</span>
</div>
innerHtml() gives:
<span>The inside</span>