开发者

Get the link from html file

开发者 https://www.devze.com 2023-04-05 12:59 出处:网络
I use htmlcleaner to parse HTML files. here is example of an html file. .......<div class=\"name\"><a href=\"http://example.com\">Name</a></div>;......

I use htmlcleaner to parse HTML files. here is example of an html file.

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

I get the word Name using this construction in my code

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()

;

But I don't understand how I can get the link in my example. I want to get the link http://exxample.com but I don't know what to do.

Please help me. I read the tutorial and used the function but can't.

P.S. Sorry for my bad English


I don't use HtmlCleaner, but according to the javadoc you do it this way:

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.: you posted clearly uncompilable code P.P.S.: why don't you use some library that creates an ordinary DOM tree from html? This way you'll be able to work with parsed document using a common-known API.

0

精彩评论

暂无评论...
验证码 换一张
取 消