2010-04-26 7 views
1

Java HTML 파서 라이브러리에서 닫기 태그 (예 : </h1>)를 처리하려면 어떻게해야합니까? 예를 들어Java Html 파서 및 닫기 태그

, 나는 다음과 같은 경우 :

public class MyFilter implements NodeFilter { 

public boolean accept(Node node) { 
    if (node instanceof TagNode) { 
    TagNode theNode = (TagNode) node; 
    if (theNode.getRawTagName().equals("h1")) { 
    return true; 
    } else { 
    return false; 
    } 
    } 
    return false; 
} 
} 

public class MyParser { 
public final String parseString(String input) { 
    Parser parser = new Parser(); 
    MyFilter theFilter = new MyFilter(); 
    parser.setInputHTML("<h1>Welcome, User</h1>"); 
    NodeList theList = parser.parse(theFilter); 
    return theList.toHtml(); 
} 
} 

내 파서를 실행하면, 내가 다시 다음과 같은 출력 얻을 :

<h1>Welcome, User</h1>Welcome, User</h1> 

노드 목록이 규모 3의 목록이 포함되어 있습니다 다음 엔터티 :

(tagNode) <h1> 

(textNode) Welcome, User 

(tagNode) </h1> 

출력을 "<h1>Welcome, User</h1>"으로하고 싶습니다. 누구나 내 샘플 파서에서 무엇이 잘못되었는지 보시겠습니까?

답변

0

힌트 :

나는 당신이이 경우에 isEndTag() API에 의존 생각합니다.

0

필터가 너무 많은 노드를 받아들입니다. 샘플 입력의 경우 <h1> 태그에 대해 노드가 하나만있는 NodeList을 작성하려고합니다. 다른 두 노드는 첫 번째 노드의 하위 노드이므로 NodeList에 추가하면 안됩니다.


다음 코드를 추가하면 문제가 무엇인지 잘 볼 수 있습니다.

for (Node node : theList.toNodeArray()) 
{ 
    System.out.println(node.toHtml()); 
} 

그것은 인쇄해야

<h1>Welcome, User</h1> 
Welcome, User 
</h1>