Reputation: 1
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
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
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
Reputation: 24791
People might suggest to use regex, but beware, you can use an XML Parser.
Upvotes: -1