Is there any library or pre-written code to remove css attributes from HTML code.
The requirement is, the Java code has to parse through the input html document, and remove the css attributes and produce the output html document.
For example if the input html document has this element,
<p class="abc" style="xyz" > some text </p>
the output should be
<p > some text </p>
Use jsoup and NodeTraversor to remove class and style attributes from all elements
Document doc = Jsoup.parse(input);
NodeTraversor traversor = new NodeTraversor(new NodeVisitor() {
@Override
public void tail(Node node, int depth) {
if (node instanceof Element) {
Element e = (Element) node;
e.removeAttr("class");
e.removeAttr("style");
}
}
@Override
public void head(Node node, int depth) {
}
});
traversor.traverse(doc.body());
String modifiedHtml = doc.toString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With