1

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

2 に答える 2

0

HtmlCleaner に詳しくありませんが、「クリーニング」のみを実行しますか? テキスト値を設定する方法が見つかりません。 http://htmlcleaner.sourceforge.net/doc/index.html

jsoup は完全な HTML パーサー (Java で記述) であり、jQuery を使用する場合と同様に DOM 要素を処理します。text() セッター メソッドを使用して、テキスト ノードを更新します。 http://jsoup.org/cookbook/modifying-data/set-text

// intitial: <div></div>
div = doc.select("div").first();
div.text("five > four");
div.prepend("First ");
div.append(" Last");
// now: <div>First five &gt; four Last</div>

jsoup (および ColdFusion) の詳細:

于 2014-05-19T17:07:14.210 に答える