Using an ObjectFindingVisitor to Extract Links

A visitor visits all links, and an ObjectFindingVisitor is designed to find one specific class of nodes, in this case LinkTag tags:

    import org.htmlparser.Node;
    import org.htmlparser.Parser;
    import org.htmlparser.tags.LinkTag;
    import org.htmlparser.util.ParserException;
    import org.htmlparser.visitors.ObjectFindingVisitor;

    public class LinkDemo
    {
        public static void main (String[] args) throws ParserException
        {
            Parser parser = new Parser ("http://urlIWantToParse.com");
            ObjectFindingVisitor visitor = new ObjectFindingVisitor (LinkTag.class);
            parser.visitAllNodesWith (visitor);
            Node[] links = visitor.getTags ();
            for (int i = 0; i < links.length; i++)
            {
                LinkTag linkTag = (LinkTag)links[i];
                System.out.print ("\"" + linkTag.getLinkText () + "\" => ");
                System.out.println (linkTag.getLink ());
            }
        }
    }

    Last edited on Wednesday, January  7, 2004  4:09:50 pm.