2011-09-18 6 views
0

HTML 파일을 구문 분석하기 위해 htmlcleaner를 사용합니다. 다음은 html 파일의 예입니다.html 파일에서 링크 가져 오기

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

나는

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

내 코드에서이 구조를 사용하여 단어 Name를 얻을;

하지만 내 사례에서 어떻게 링크를 얻을 수 있는지 이해할 수 없습니다. 나는 http://exxample.com 링크를 얻고 싶지만 무엇을 해야할지 모르겠다.

도와주세요. 나는 튜토리얼을 읽고 함수를 사용했으나 할 수 없었다.

P. 내 나쁜 영어 죄송합니다

답변

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); 
} 

PS를 : 당신이 명확하게 uncompilable 코드 PPS를 게시 : 왜 사용하지 않는 HTML에서 일반 DOM 트리를 만드는 라이브러리? 이렇게하면 잘 알려진 API를 사용하여 구문 분석 된 문서로 작업 할 수 있습니다.

+0

네 대답은 ... 네 .... 나는 내가 사용하는 모든 코드를 복사하지 않는다는 것을 알지만 ... 응용 프로그램은 작동합니다 .... 내 문제는 웹 사이트에서 링크를 얻습니다. .HTMLcleaner는 안드로이드에서 html을 파싱 할 때 사용하기에 더 쉽습니다. & htmlcleaner를 사용하여 일반 DOM 트리를 만들 수 있습니다 ...) 시도해보십시오.) –

관련 문제