can not find symbol class Head

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Merin Lalu
    New Member
    • Oct 2011
    • 20

    can not find symbol class Head

    Can any one help me?

    I got a code for the parse method of Parser class in html parser library from the link

    htmlparser.sour ceforge.net/javadoc/index.html

    the code is as follows

    Code:
    NodeList nl = parser.parse (null); // here is your two node list
     NodeList heads = nl.extractAllNodesThatMatch (new TagNameFilter ("HEAD"))
     if (heads.size () > 0) // there may not be a HEAD tag
     {
         Head head = heads.elementAt (0); // there should be only one
         head.removeAll (); // clean out the contents
         Tag title = new TitleTag ();
         title.setTagName ("title");
         title.setChildren (new NodeList (new TextNode ("The New Title")));
         Tag title_end = new TitleTag ();
         title_end.setTagName ("/title");
         title.setEndTag (title_end);
         head.add (title);
     }
     System.out.println (nl.toHtml ()); // output the modified HTML
    but when i am running this code, i am getting an error

    "cannot find symbol class Head"

    i couldnt find a class named Head in the library..

    what should i do??
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    I think it was a typing mistake in the code.
    I suspect the line should have been
    Code:
    Tag head = heads.elementAt (0); // there should be only one
    instead of
    Code:
    Head head = heads.elementAt (0); // there should be only one

    Comment

    • Merin Lalu
      New Member
      • Oct 2011
      • 20

      #3
      But the Tag class does not have a method named removeAll(). so again error

      can not find symbol method removeAll()

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        I thought it could be a HeadTag but that doesn't seem to contain a removeAll method either.
        Find out what type it is with
        Code:
         System.out.println(heads.elementAt (0));
        //comment out the rest of the code

        Comment

        • Merin Lalu
          New Member
          • Oct 2011
          • 20

          #5
          the nodelist heads is null.. so heads.elementAt (0) is null

          but input htmlpage has an headtag in it. i dont know why it is printing null

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            But there is an
            Code:
            if (heads.size () > 0) // there may not be a HEAD tag
             {
            If your code is going into that if then a HEAD tag is there.
            Create a short version of the program and post the code and the html you are using.

            Comment

            • Merin Lalu
              New Member
              • Oct 2011
              • 20

              #7
              actually i commented
              Code:
              if (heads.size () > 0){
              }
              I am submitting the code and HTML code

              Code:
               import org.htmlparser.Parser;
               import org.htmlparser.Tag;
               import org.htmlparser.Text;
               import org.htmlparser.util.ParserException;
               import org.htmlparser.visitors.NodeVisitor;
               import java.util.*;
              import org.htmlparser.filters.TagNameFilter;
              import org.htmlparser.nodes.TextNode;
              import org.htmlparser.NodeFilter;
              import org.htmlparser.util.NodeList;
              
              
               public class MyVisitor1 extends NodeVisitor
               {
                   
              String str="DOCUMENT/HTML"; 
              static Hashtable pages = new Hashtable();
              int i=1;
              static Enumeration names; 
              
              static String str1=null;
              
                   public MyVisitor1 ()
                   {
                   
                   }
                   public void visitTag (Tag tag)
                   {
                     //  System.out.println ("\n" + tag.getTagName ());
              
                       str=str+"/"+tag.getTagName();
              
                       pages.put("p"+i,str);
                       i=i+1;
                     
                   //    System.out.println ("\n" +str);
                   }
                   public void visitStringNode (Text string)
                   {
              
              	String st=string.getText();
              
              	if(!st.trim().equals(""))
              	{
              	
              	str=str+"/"+string.getText();
               	pages.put("p"+i,str); 
              	i=i+1; 
              
              
              	}
                   }
              
                   public static void display()
                  {
              
              	names = pages.keys();
              	
              	while(names.hasMoreElements()) 
              	{
              	str1 = (String) names.nextElement();
              	System.out.println("...................................................................\n");
              	System.out.println(str1+ ": " +pages.get(str1));
              	} 
              System.out.println("the size of hash table is ::"+pages.size());
                  }
              
                   public void visitEndTag(Tag tag)
                  {
              	String starttag=tag.getTagName();
              	int n=str.lastIndexOf(starttag);
              	//System.out.println("last index is::"+n);
              
              	if(n<0)
              	{
              		return;
              	}	
              	str= str.substring(0,n-1);
              	//System.out.println("substring is::"+str);
              		  
                    }
              
                   public static void main (String[] args) throws ParserException
                   {
                      
                    	Parser parser = new Parser ("E:/project/docs/d6.html");
              	
              	NodeList nl = parser.parse (null); // here is your two node list
              	 NodeList heads = nl.extractAllNodesThatMatch (new TagNameFilter ("head"));
              
               	//if (heads.size () > 0) // there may not be a HEAD tag
               	//{
              	System.out.println("hello"+heads.elementAt (0));
                	   //Tag head =(Tag)heads.elementAt (0); // there should be only one
                 	 //head.removeAll (); // clean out the contents
              
              	//}
              
                       MyVisitor1 visitor = new MyVisitor1 ();
              
                       nl.visitAllNodesWith (visitor);
              	//display();
                   }
               }
              here is the html inout

              Code:
              <html>
              <head>
              <title> hai</title>
              </head>
              <body>
              <h1>tech</h1>
              <br />
              </body>
              </html>

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Use
                Code:
                NodeList heads = nl.extractAllNodesThatMatch(new TagNameFilter("HEAD"), true);
                Tag head =(Tag)heads.elementAt (0); // there should be only one
                System.out.println(head);
                You'd have to check the Javadocs to see what that true flag does.

                Comment

                • Merin Lalu
                  New Member
                  • Oct 2011
                  • 20

                  #9
                  Thanks a lot.. I got it..

                  Comment

                  Working...