Results 1 to 4 of 4
Thread: html parser to retrieve <img>
- 03-27-2012, 03:19 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
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); } }
It says that is incompatible type. Anyone can help on this? thanks in advance.
- 03-27-2012, 03:23 PM #2
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.
- 03-27-2012, 03:27 PM #3
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 - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 03-27-2012, 03:33 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
HTML parser
By Yuuki in forum New To JavaReplies: 4Last Post: 10-25-2010, 12:32 PM -
problems with html parser
By vitaly87 in forum Advanced JavaReplies: 0Last Post: 03-13-2010, 02:37 PM -
Problem with HTML parser
By kpraveenreddy in forum New To JavaReplies: 0Last Post: 03-02-2010, 03:12 PM -
Jericho HTML Parser 2.6
By Java Tip in forum Java SoftwareReplies: 0Last Post: 06-26-2008, 07:22 PM -
Jericho HTML Parser 2.4
By levent in forum Java SoftwareReplies: 0Last Post: 05-21-2007, 11:05 AM
Bookmarks