Results 1 to 4 of 4
  1. #1
    jianrenyizhu is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default html parser to retrieve <img>

    Hi all,
    i currently have this code that parses the webpage and it will print out those the lines with <img>.
    However, it seems to have an error when i build the project.

    This is my code:

    Java Code:
    import javax.swing.text.html.*;
    import javax.swing.text.Element;
    import javax.swing.text.ElementIterator;
    import java.net.URL;
    import java.io.InputStreamReader;
    import java.io.Reader;
    
    /**
     *  Extract all "img" tags from an HTML document.
     */
    public class HTMLParser
    {
      public static void main( String[] argv ) throws Exception
      {
        URL url = new URL( "http://java.sun.com" ); 
        HTMLEditorKit kit = new HTMLEditorKit(); 
        HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); 
        doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
        Reader HTMLReader = new InputStreamReader(url.openConnection().getInputStream()); 
        kit.read(HTMLReader, doc, 0); 
    
        //  Get an iterator for all HTML tags.
        ElementIterator it = new ElementIterator(doc); 
        Element elem; 
        
        while( elem = it.next() != null  )
        { 
          if( elem.getName().equals(  "img") )
          { 
            String s = (String) elem.getAttributes().getAttribute(HTML.Attribute.SRC);
            if( s != null ) 
              System.out.println (s );
          } 
        }
        System.exit(0);
      }
    }
    The error happens on the line while( elem = it.next() != null ).
    It says that is incompatible type. Anyone can help on this? thanks in advance.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: html parser to retrieve <img>

    If you add parenthesis to the expression it should correct the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: html parser to retrieve <img>

    Why:
    while( elem = it.next() != null )

    Split that up, or separate it using parenthesis.

    Edit- Too slow!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    jianrenyizhu is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: html parser to retrieve <img>

    thx all for the replies! it works now! much thanks!

Similar Threads

  1. HTML parser
    By Yuuki in forum New To Java
    Replies: 4
    Last Post: 10-25-2010, 11:32 AM
  2. problems with html parser
    By vitaly87 in forum Advanced Java
    Replies: 0
    Last Post: 03-13-2010, 01:37 PM
  3. Problem with HTML parser
    By kpraveenreddy in forum New To Java
    Replies: 0
    Last Post: 03-02-2010, 02:12 PM
  4. Jericho HTML Parser 2.6
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 06-26-2008, 06:22 PM
  5. Jericho HTML Parser 2.4
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-21-2007, 10:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •