我想要搜索特定關鍵字的DOM,找到它時,我想知道它來自樹中的哪個節點。查找節點中的關鍵字並獲取DOM中的節點名稱
static void search(String segment, String keyword) {
if (segment == null)
return;
Pattern p=Pattern.compile(keyword,Pattern.CASE_INSENSITIVE);
StringBuffer test=new StringBuffer (segment);
matcher=p.matcher(test);
if(!matcher.hitEnd()){
total++;
if(matcher.find())
//what to do here to get the node?
}
}
public static void traverse(Node node) {
if (node == null || node.getNodeName() == null)
return;
search(node.getNodeValue(), "java");
check(node.getFirstChild());
System.out.println(node.getNodeValue() != null &&
node.getNodeValue().trim().length() == 0 ? "" : node);
check(node.getNextSibling());
}
這是什麼語言?它看起來像Java或可能C#,如果你在問題標籤中包含語言,你將更有可能得到答案。 – kamui
是它的Java和感謝您的評論:) – lonesome
我發現很難理解你想達到什麼,但可能你想從搜索返回一個布爾值來表明該令牌已被找到。如果你指定更好的目標,這將更容易幫助。 – Miquel