Menu

Getting the Attributes of body and frame tag

Vinoth K G
2004-06-26
2004-06-28
  • Vinoth K G

    Vinoth K G - 2004-06-26

          I want to parse an HTML file in order to get the background property in the body tag and src property of the Frame tag. Here is the sample program I tried, but I didn't get.
    **************************************
    import java.util.Iterator;
    import java.util.LinkedList;

    import org.htmlparser.Parser;
    import org.htmlparser.tags.*;
    import org.htmlparser.util.ParserException;
    import org.htmlparser.visitors.NodeVisitor;

    public class LSParserVisitor extends NodeVisitor {

        private LinkedList listOfLinks;

        {
            listOfLinks = new LinkedList();
        }

        public LSParserVisitor() {
        }
       
        public void visitTag(BodyTag bodyTag){
               listOfLinks.add(bodyTag);
        }
        public void visitTag(FrameTag frameTag){
               listOfLinks.add(frameTag);
        }

        public LinkedList getListOfLinks() {
            return listOfLinks;
        }

        public static void main(String[] args) {
            try {
                LSParserVisitor parserVisitor = new LSParserVisitor();
                Parser parser = new Parser(
                "F:\\vinoth\\docs\\api\\index.html");
                parser.visitAllNodesWith(parserVisitor);
                Iterator itr = parserVisitor.getListOfLinks().iterator();
                while (itr.hasNext()) {
                   Object ob=itr.next();
                    if(ob instanceof BodyTag){
                    System.out.println("Body Tag "+((BodyTag)ob).getAttribute("Background"));
                   }else if(ob instanceof FrameTag){
                    System.out.println("Frame Tag "+((FrameTag)ob).getAttribute("src"));
                   }

                }
            } catch (ParserException pe) {
                System.out.println("ParserException" + pe.getMessage());
            }
        }
    }
    ************************************
    I don't know what is the problem with this code.
    Could  somebody help me and give me a solution for this problem.

     
    • Derrick Oswald

      Derrick Oswald - 2004-06-28

      You'll need to override the visitTag(Tag x) method of NodeVisitor. The signatures you have don't match any of the methods in NodeVisitor. Check for the tag's name matching those you're interested in:
      LSParserVisitor...
          public void visitTag (Tag tag)
          {
              if (tag.getName ().equals ("BODY"))
                   listOfLinks.add (tag);
              else  if (tag.getName ().equals ("FRAME"))
                   listOfLinks.add (tag);
          }

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.