E poi non dite che non vi voglio bene.....
Allora.
Ho trovato ed analizzato questo codice.
Praticamente ( senza servlet - imparato cosa nuova anche io )
Preleva il codice html di indirizzo dato.
La cosa particolare di questo codice è la presenza dei singoli elementi, quindi non ritornerà <html>, ma solo html.
Allo stesso modo vengono recuperati i parametri e vengono recuperati gli href.
Classe importante Reader e package html
codice:
import java.io.*;
import java.net.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
class GetLinks
{
public static void main(String[] args)
{
EditorKit kit = new HTMLEditorKit();
Document doc = kit.createDefaultDocument();
// The Document class does not yet handle charset's properly.
doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
try
{
// Create a reader on the HTML content.
Reader rd = getReader( "http://www.google.it" );
// Parse the HTML.
kit.read(rd, doc, 0);
// Iterate through the elements of the HTML document.
ElementIterator it = new ElementIterator(doc);
Element elem;
while ( (elem = it.next()) != null )
{
System.out.println( elem.getName() );
SimpleAttributeSet s =
(SimpleAttributeSet)elem.getAttributes().getAttribute(HTML.Tag.A);
if (s != null)
{
System.out.println( s );
// disiplay attribute value
System.out.println( s.getAttribute(HTML.Attribute.HREF) );
// display tag text
int start = elem.getStartOffset();
System.out.println( elem.getDocument().getText(start,
elem.getEndOffset() - start) );
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
System.exit(1);
}
// Returns a reader on the HTML data. If 'uri' begins
// with "http:", it's treated as a URL; otherwise,
// it's assumed to be a local filename.
static Reader getReader(String uri)
throws IOException
{
// Retrieve from Internet.
if (uri.startsWith("http:"))
{
URLConnection conn = new URL(uri).openConnection();
return new InputStreamReader(conn.getInputStream());
}
// Retrieve from file.
else
{
return new FileReader(uri);
}
}
}