By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,796 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,796 IT Pros & Developers. It's quick & easy.

can not find symbol class Head

P: 20
Can any one help me?

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

htmlparser.sourceforge.net/javadoc/index.html

the code is as follows

Expand|Select|Wrap|Line Numbers
  1. NodeList nl = parser.parse (null); // here is your two node list
  2.  NodeList heads = nl.extractAllNodesThatMatch (new TagNameFilter ("HEAD"))
  3.  if (heads.size () > 0) // there may not be a HEAD tag
  4.  {
  5.      Head head = heads.elementAt (0); // there should be only one
  6.      head.removeAll (); // clean out the contents
  7.      Tag title = new TitleTag ();
  8.      title.setTagName ("title");
  9.      title.setChildren (new NodeList (new TextNode ("The New Title")));
  10.      Tag title_end = new TitleTag ();
  11.      title_end.setTagName ("/title");
  12.      title.setEndTag (title_end);
  13.      head.add (title);
  14.  }
  15.  System.out.println (nl.toHtml ()); // output the modified HTML
  16.  
  17.  
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??
Mar 15 '12 #1
Share this Question
Share on Google+
8 Replies


10K+
P: 13,264
I think it was a typing mistake in the code.
I suspect the line should have been
Expand|Select|Wrap|Line Numbers
  1. Tag head = heads.elementAt (0); // there should be only one
  2.  
instead of
Expand|Select|Wrap|Line Numbers
  1. Head head = heads.elementAt (0); // there should be only one
  2.  
Mar 15 '12 #2

P: 20
But the Tag class does not have a method named removeAll(). so again error

can not find symbol method removeAll()
Mar 15 '12 #3

10K+
P: 13,264
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
Expand|Select|Wrap|Line Numbers
  1.  System.out.println(heads.elementAt (0));
  2. //comment out the rest of the code
  3.  
Mar 15 '12 #4

P: 20
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
Mar 15 '12 #5

10K+
P: 13,264
But there is an
Expand|Select|Wrap|Line Numbers
  1. if (heads.size () > 0) // there may not be a HEAD tag
  2.  {
  3.  
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.
Mar 15 '12 #6

P: 20
actually i commented
Expand|Select|Wrap|Line Numbers
  1. if (heads.size () > 0){
  2. }
I am submitting the code and HTML code

Expand|Select|Wrap|Line Numbers
  1.  
  2.  import org.htmlparser.Parser;
  3.  import org.htmlparser.Tag;
  4.  import org.htmlparser.Text;
  5.  import org.htmlparser.util.ParserException;
  6.  import org.htmlparser.visitors.NodeVisitor;
  7.  import java.util.*;
  8. import org.htmlparser.filters.TagNameFilter;
  9. import org.htmlparser.nodes.TextNode;
  10. import org.htmlparser.NodeFilter;
  11. import org.htmlparser.util.NodeList;
  12.  
  13.  
  14.  public class MyVisitor1 extends NodeVisitor
  15.  {
  16.  
  17. String str="DOCUMENT/HTML"; 
  18. static Hashtable pages = new Hashtable();
  19. int i=1;
  20. static Enumeration names; 
  21.  
  22. static String str1=null;
  23.  
  24.      public MyVisitor1 ()
  25.      {
  26.  
  27.      }
  28.      public void visitTag (Tag tag)
  29.      {
  30.        //  System.out.println ("\n" + tag.getTagName ());
  31.  
  32.          str=str+"/"+tag.getTagName();
  33.  
  34.          pages.put("p"+i,str);
  35.          i=i+1;
  36.  
  37.      //    System.out.println ("\n" +str);
  38.      }
  39.      public void visitStringNode (Text string)
  40.      {
  41.  
  42.     String st=string.getText();
  43.  
  44.     if(!st.trim().equals(""))
  45.     {
  46.  
  47.     str=str+"/"+string.getText();
  48.      pages.put("p"+i,str); 
  49.     i=i+1; 
  50.  
  51.  
  52.     }
  53.      }
  54.  
  55.      public static void display()
  56.     {
  57.  
  58.     names = pages.keys();
  59.  
  60.     while(names.hasMoreElements()) 
  61.     {
  62.     str1 = (String) names.nextElement();
  63.     System.out.println("...................................................................\n");
  64.     System.out.println(str1+ ": " +pages.get(str1));
  65.     } 
  66. System.out.println("the size of hash table is ::"+pages.size());
  67.     }
  68.  
  69.      public void visitEndTag(Tag tag)
  70.     {
  71.     String starttag=tag.getTagName();
  72.     int n=str.lastIndexOf(starttag);
  73.     //System.out.println("last index is::"+n);
  74.  
  75.     if(n<0)
  76.     {
  77.         return;
  78.     }    
  79.     str= str.substring(0,n-1);
  80.     //System.out.println("substring is::"+str);
  81.  
  82.       }
  83.  
  84.      public static void main (String[] args) throws ParserException
  85.      {
  86.  
  87.           Parser parser = new Parser ("E:/project/docs/d6.html");
  88.  
  89.     NodeList nl = parser.parse (null); // here is your two node list
  90.      NodeList heads = nl.extractAllNodesThatMatch (new TagNameFilter ("head"));
  91.  
  92.      //if (heads.size () > 0) // there may not be a HEAD tag
  93.      //{
  94.     System.out.println("hello"+heads.elementAt (0));
  95.          //Tag head =(Tag)heads.elementAt (0); // there should be only one
  96.         //head.removeAll (); // clean out the contents
  97.  
  98.     //}
  99.  
  100.          MyVisitor1 visitor = new MyVisitor1 ();
  101.  
  102.          nl.visitAllNodesWith (visitor);
  103.     //display();
  104.      }
  105.  }  
  106.  
  107.  
here is the html inout

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3. <head>
  4. <title> hai</title>
  5. </head>
  6. <body>
  7. <h1>tech</h1>
  8. <br />
  9. </body>
  10. </html>
  11.  
  12.  
Mar 15 '12 #7

10K+
P: 13,264
Use
Expand|Select|Wrap|Line Numbers
  1. NodeList heads = nl.extractAllNodesThatMatch(new TagNameFilter("HEAD"), true);
  2. Tag head =(Tag)heads.elementAt (0); // there should be only one
  3. System.out.println(head);
  4.  
You'd have to check the Javadocs to see what that true flag does.
Mar 15 '12 #8

P: 20
Thanks a lot.. I got it..
Mar 15 '12 #9

Post your reply

Sign in to post your reply or Sign up for a free account.