title image


Smiley Re: Verbindung zu URL herstellen
Hi!

habe das Programm gerade mal geschrieben, brauche es evtl. bal sowieso.



*********************************************************



import java.io.InputStreamReader;



import java.net.URL;

import java.net.URLConnection;



import javax.swing.text.MutableAttributeSet;

import javax.swing.text.html.HTMLEditorKit;

import javax.swing.text.html.HTML;

import javax.swing.text.html.parser.ParserDelegator;



import java.util.LinkedList;



class MyLink

{

private String href = null;

private StringBuffer text = null;



public MyLink()

{

text=new StringBuffer();

}



public void setHref(String href)

{

this.href=href;

}



public void addText(char[] text)

{

this.text.append(text);

}



public String getText()

{

return text.toString();

}



public String getHref()

{

return href;

}

}



class MyParserCallback extends HTMLEditorKit.ParserCallback

{

private MyLink actLink = null;

private LinkedList liste = null;



public MyParserCallback ()

{

liste = new LinkedList();

}



public void handleStartTag(HTML.Tag tag, MutableAttributeSet attributes, int pos)

{

if (tag==HTML.Tag.A)

{

actLink = new MyLink();

actLink.setHref((String)(attributes.getAttribute(HTML.Attribute.HREF)));

}

}



public void handleEndTag(HTML.Tag tag, int pos)

{

if (tag==HTML.Tag.A)

{

liste.add(actLink);

actLink=null;

}

}



public void handleText(char[] data, int pos)

{

if (actLink != null)

{

actLink.addText(data);

}

}



public MyLink[] getLinkArray()

{

return (MyLink[])(liste.toArray(new MyLink[liste.size()]));

}

}



public class LinkReader

{

public static void main(String[] args)

{

try

{

URL url = new URL(args[0]);

URLConnection urlConn = url.openConnection();

ParserDelegator parser = new ParserDelegator();

MyParserCallback parserCallback = new MyParserCallback();

parser.parse(new InputStreamReader(urlConn.getInputStream()), parserCallback, true);

MyLink[] linkArray = parserCallback.getLinkArray();

for (int i=0; i
{

System.out.println("HREF: "+linkArray[i].getHref());

System.out.println("TEXT: "+linkArray[i].getText());

System.out.println("---------------------------------------------------------");

}

}

catch (Exception e)

{

System.out.println("Exception: "+e.getMessage());

}

}

}



*********************************************************



Der Aufruf lautet dann

java LinkReader "http://www.spotlight.de"



Grße,

Uwe
Visit www.uwenowak.de

geschrieben von

Login

E-Mail:
  

Passwort:
  

Beitrag anfügen

Symbol:
 
 
 
 
 
 
 
 
 
 
 
 
 

Überschrift: