Dave's Brain

Browse - Programming Tips - Using jsoup how can I get the outerHtml() without the innerHtml() ?

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>

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.