Click here to Skip to main content
15,913,685 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, i have this code that i acquired help to parse a html web page and print all the <img> tag lines out.However, i'm having a slight issue with the codes.

Java
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);
  }
} 


it keeps showing an error on the line
while( elem = it.next() != null  )
saying incompatible types. Anyone can help on this? thx in advance.
Posted
Updated 10-Feb-22 20:21pm

I get 2 errors:

Type mismatch: cannot convert from boolean to Element
Type mismatch: cannot convert from Element to boolean


both at that line. Now what to do?

Well, it's considered to be bad style to init Objects like that and to compare there value at the same time.

so let's change it:

Java
while(true) // ever lasting...
    { 
       elem = it.next();
      if(elem == null) break; //..until the end is near.
      if( elem.getName().equals(  "img") )
      { 
        String s = (String) elem.getAttributes().getAttribute(HTML.Attribute.SRC);
        if( s != null ) 
          System.out.println (s );
      } 
    }
 
Share this answer
 
Please You need to put extra brackets as this
Java
while( ( elem = it.next() ) != null  )
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900