Ver Mensaje Individual
  #1 (permalink)  
Antiguo 17/02/2010, 05:08
SeNNiNG
 
Fecha de Ingreso: febrero-2006
Mensajes: 21
Antigüedad: 18 años, 1 mes
Puntos: 0
Extraer enlaces HTML (resuelto)

Buenas,
Estoy realizando una aplicación que gestiona la búsqueda y descarga de podcasts. Para la búsqueda, necesito extraer los enlaces, para ello utilizo el HTMLEditorKit y extraigo las etiquetas <A> de la siguiente forma:

(El parámetro de entrada es el documento html convertido en string.)
Código:
public static LinkedList getLinks(String texto) {

           LinkedList result = new LinkedList();
           try
           {
               HTMLEditorKit kit = new HTMLEditorKit();
               HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
               doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
               StringReader sr = new StringReader(texto);
               kit.read(sr, doc, 0);

               HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
               
               while (it.isValid())
               {                   
                    SimpleAttributeSet s = (SimpleAttributeSet)it.getAttributes();                    
                    String link = (String)s.getAttribute(HTML.Attribute.HREF);
                    if (link != null) {
                        // Agregamos el resultado a la lista
                        if(link.indexOf("localhost")<=0) {
                            result.add(link);
                        }//if
                    }//if
                    it.next();
                }//while
           }//try
           catch (Exception ex)
           {
               System.out.println(ex);
               return null;
           }//catch
           return result;
    }//getLinks
Pero cuando quiero extraer los enlaces de tipo <LINK> la función retorna null.

He comprobado si (HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);) it tenía algún valor pero es nulo, así que ahí está el problema pero no sé cómo solucionarlo.
He mirado en google pero todos los ejemplos están hechos con la etiqueta <A>.

Un saludo y gracias de antemano

Última edición por SeNNiNG; 21/02/2010 a las 10:19