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