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