【问题标题】:Get the link from html file从 html 文件中获取链接
【发布时间】:2011-09-18 20:15:59
【问题描述】:

我使用 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,但我不知道该怎么做。

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

附:对不起我的英语不好

【问题讨论】:

    标签: java android html parsing htmlcleaner


    【解决方案1】:

    我不使用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 处理已解析的文档。

    【讨论】:

    • 谢谢你的回答)是的......我注意到我没有在这里复制我使用的所有代码......但是应用程序可以工作......我的问题是来自网站的链接...在android中解析html时,Htmlcleaner更易于使用...&我可以使用htmlcleaner创建一个普通的DOM树...)我试试)
    猜你喜欢
    • 2015-07-19
    • 2012-05-21
    • 2015-01-03
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 2011-07-19
    • 2020-06-30
    • 2022-01-02
    相关资源
    最近更新 更多