假设我正在使用HtmlCleaner ( v2.18 ) 清理一些 html,并且我想将属性invalidAttributeNamePrefix
(请参阅Cleaner 参数部分)设置为某个值,即: data-
.
这样my-custom-attr="my-value"
,HTML 中的属性将被转换为data-my-custom-attr="my-value"
.
我怎样才能做到这一点?我找不到Java 用法的任何示例。
您可以将这段代码作为参考:
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties properties = cleaner.getProperties();
properties.setOmitComments(true);
// properties.setInvalidAttributeNamePrefix("data-"); there is no such method
// html is a declared variable which contains some html content
TagNode rootTagNode = cleaner.clean(html);
XmlSerializer xmlSerializer = new PrettyXmlSerializer(properties);
String cleanedHtml = xmlSerializer.getAsString(rootTagNode);