在java中删除HTML中的css信息 [英] Removing css information from HTML in java

查看:138
本文介绍了在java中删除HTML中的css信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要的是,Java代码必须通过输入html文档进行解析。

Is there any library or pre-written code to remove css attributes from HTML code.

有没有库或预先编写的代码可以从HTML代码中删除css属性。 ,并删除css属性并生成输出html文档。

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.

例如,如果输入的html文档包含这个元素,

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>


推荐答案

使用jsoup和NodeTraversor从类中删除类和样式属性所有元素

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();

这篇关于在java中删除HTML中的css信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆