我正在使用带有 ColdFusion 的 HtmlCleaner。在下面的代码中,我正在遍历节点树并寻找内容节点。我想要做的是能够修改节点的文本内容。
node.traverse(new TagNodeVisitor() {
public boolean visit(TagNode tagNode, HtmlNode htmlNode) {
if (htmlNode instanceof ContentNode) {
ContentNode content = ((ContentNode) htmlNode);
String textContent = content.getContent();
}
// tells visitor to continue traversing the DOM tree
return true;
}
});
我正在使用的示例是:
// traverse whole DOM and update images to absolute URLs
node.traverse(new TagNodeVisitor() {
public boolean visit(TagNode tagNode, HtmlNode htmlNode) {
if (htmlNode instanceof TagNode) {
TagNode tag = (TagNode) htmlNode;
String tagName = tag.getName();
if ("img".equals(tagName)) {
String src = tag.getAttributeByName("src");
if (src != null) {
tag.setAttribute("src", Utils.fullUrl(siteUrl, src));
}
}
} else if (htmlNode instanceof CommentNode) {
CommentNode comment = ((CommentNode) htmlNode);
comment.getContent().append(" -- By HtmlCleaner");
}
// tells visitor to continue traversing the DOM tree
return true;
}
});