nidhi
nidhi

Reputation: 1

how to remove attribute from html tags in java

I wanted to remove the particular attribute from anchor tag:

<a  id="nav-askquestion" style="cursor:default" href="/questions">

output:-

<a   href="/questions">

through java program

Upvotes: 0

Views: 3861

Answers (3)

felixsigl
felixsigl

Reputation: 2920

we use htmlparser for this kind of job

you can parse and modify nodes with this untested snipplet:

NodeVisitor visitor = new NodeVisitor() {
    public void visitTag(Tag tag) {
            tag.removeAttribute("id");
            tag.removeAttribute("style");
    }

};

Parser parser = new Parser(...);
parser.visitAllNodesWith(visitor);

Upvotes: 4

Stephan
Stephan

Reputation: 43013

This little snippet will do the trick.

Ask me if you need some questions about the Regex


public class test {

    public static void main(String[] args) {
        String htmlFragment ="<a  id=\"nav-askquestion\" style=\"cursor:default\" href=\"/questions\">";
        String attributesToRemove = "id|style";

        System.out.println(htmlFragment);
        System.out.println(cleanHtmlFragment(htmlFragment, attributesToRemove));
    }

    private static String cleanHtmlFragment(String htmlFragment, String attributesToRemove) {
        return htmlFragment.replaceAll("\\s+(?:" + attributesToRemove + ")\\s*=\\s*\"[^\"]*\"","");    
    }
}

Upvotes: 2

Suraj Chandran
Suraj Chandran

Reputation: 24791

People might suggest to use regex, but beware, you can use an XML Parser.

Upvotes: -1

Related Questions