seeExtracting Links from an HTML File
http://developer.java.sun.com/developer/TechTips/1999/tt0923.html

解决方案 »

  1.   

    try
        {
          Reader rd = new InputStreamReader(in);
          EditorKit kit = new HTMLEditorKit();
          HTMLDocument doc = (HTMLDocument)kit.createDefaultDocument();
          // The Document class does not yet handle charset's properly.
          doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
          kit.read(rd, doc, 0);      // Find all the A elements in the HTML document
          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) {
                 // Add the link to the result list
                  //result.add(link);
              System.out.println(link);
             }
              it.next();
          }
        }
       catch (MalformedURLException e)
       {
       }
       catch (BadLocationException e)
       {
       }
       catch (IOException e)
       {
       }
    我用上面的代码输InputStream里面的链接,但是却只显示了/a/b.html,而不是显示"/a/b.html mylink,我需要得到相应的mylink该怎么办呢?
      

  2.   

    if (s != null) 
    {
              System.out.println(
                s.getAttribute(HTML.Attribute.HREF) );  System.out.println(doc.getText(elem.getStartOffset(),
    elem.getEndOffset() - elem.getStartOffset()));}