tags:

views:

154

answers:

1
+1  Q: 

htmlParser Help

UPDATE: Hi Pascal, Thanks for the quick reply, This is almost what I wanted. The newlink is different for each tag, can you please help me to do that.

Thanks Micheal

All i need to do is iterate over all the link tags that appear in the input String, grab their value, and replace with a different link with out disturbing the link text

Any help would be greatly appreciated.

Thanks

Hi,

I am new using htmlParser in Java, please help me with this condition.

htmlString =  <a class="user" href="">first name</a> posted on <a class="user" href="">Test Test</a>'s wiki entry, <a href="http://localhost:8080/b/lll/ddd"&gt;werwrwrwerwerwer&lt;/a&gt;, in

I need to replace the href link in <a class="user" href=""> to another link in the tag.

Thanks, Micheal

+3  A: 

If you are using htmlparser as HTML parser, you can do some transformations with visitors.

For example, you could create your own NodeVisitor to visit a tags:

public class MyLinkVisitor extends NodeVisitor {

    public MyLinkVisitor() { }

    public void visitTag(Tag tag) {
        if (tag.getTagName().equals("A")) {
            LinkTag link = (LinkTag) tag;
            link.setLink("http://newLink/"); //implement your logic here
        }
    }
}

Then, create a Parser, parse the HTML string and visit the returned node list:

Parser parser = new Parser(htmlString);
NodeList nl = parser.parse(null);
nl.visitAllNodesWith(new MyLinkVisitor());
System.out.println(nl.toHtml());

This is just one way to do it.

Pascal Thivent
Hi Pascal, Thanks for the quick reply, This is almost what I wanted. The newlink is different for each tag, can you please help me to do that.ThanksMicheal
@Michael As I wrote in the line comment, you have to "implement your logic here". I can help you if you give me some hints on the logic but I can't guess what you want to do exactly. So please, clarify your needs.
Pascal Thivent
Pascal, I got it.. thanks for your help..