Prilikom parsiranja html dokumenta za zadati tip taga pozivom metode getIterator(HTML.Tag.naziv_taga) moguce je ocitati sadrzaje tih tagova i vrednosti njihovih atributa.
Medjutim, ako zatrazim IMG tagove , dokument mi ne vraca ni jedan iako u njemu postoje.Da li je neko imao slican problem i kako se moze resiti?
Evo , probajte i sami(pokusajte druge tagove):
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
public class DocumentIteratorExample
{
DocumentIteratorExample(String tt) throws BadLocationException, IOException
{
URL url = new URL(tt);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument)htmlKit.createDefaultDocument();
HTMLEditorKit.Parser parser = new ParserDelegator();
HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
parser.parse(br, callback, true);
for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.IMG); iterator.isValid(); iterator.next())
{
System.out.println("Img found");
}
System.out.println("Finished");
}
public static void main(String args[]) throws Exception
{
DocumentIteratorExample doc = new DocumentIteratorExample("http://www.b92.net");
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
public class DocumentIteratorExample
{
DocumentIteratorExample(String tt) throws BadLocationException, IOException
{
URL url = new URL(tt);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument)htmlKit.createDefaultDocument();
HTMLEditorKit.Parser parser = new ParserDelegator();
HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
parser.parse(br, callback, true);
for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.IMG); iterator.isValid(); iterator.next())
{
System.out.println("Img found");
}
System.out.println("Finished");
}
public static void main(String args[]) throws Exception
{
DocumentIteratorExample doc = new DocumentIteratorExample("http://www.b92.net");
}
}
[Ovu poruku je menjao veljaradenkovic dana 04.11.2005. u 10:49 GMT+1]