从html文件中获取链接

时间:2011-09-18 20:16:00

标签: java android html parsing htmlcleaner

我使用htmlcleaner来解析HTML文件。这是一个html文件的例子。

.......<div class="name"><a href="http://example.com">Name</a></div>;...... 

我在代码

中使用了这个结构Name
HtmlCleaner cleaner = new HtmlCleaner();
            CleanerProperties props = cleaner.getProperties();
            props.setAllowHtmlInsideAttributes(true);
            props.setAllowMultiWordAttributes(true);
            props.setRecognizeUnicodeChars(true);
            props.setOmitComments(true);
            rootNode = cleaner.clean(htmlPage);
TagNode linkElements[] = rootNode.getElementsByName("div",true);
            for (int i = 0; linkElements != null && i < linkElements.length; i++)
            {
            String classType = linkElements.getAttributeByName("name");
              if (classType != null)
              {
                  if(classType.equals(class)&& classType.equals(CSSClassname)) {  linkList.add(linkElements); }
                }

                System.out.println("TagNode" + linkElements.getText());
               linkList.add(linkElements);
            }
            and then add all of this name's to listview using
TagNode=linkelements.getText().toString()

但我不明白如何在我的例子中获得链接。我想获得链接http://exxample.com,但我不知道该怎么做。

请帮帮我。我阅读了教程并使用了该功能但不能。

P.S。抱歉我的英文不好

1 个答案:

答案 0 :(得分:0)

我不使用HtmlCleaner,但根据javadoc你这样做:

List<String> links = new ArrayList<String> ();
for (TagNode aTag : linkElements[i].getElementListByName ("a", false))
{
    String link = aTag.getAttributeByName ("href");
    if (link != null && link.length () > 0) links.add (link);
}

P.S。:你发布了明显无法编译的代码 P.P.S。:为什么不使用一些从html创建普通DOM树的库?这样,您就可以使用通用的API来处理已分析的文档。