0

I have to search for specific element in DOM Structure recursively, but right now I am completely "stuck" and cannot find where my algorithm is mistaken.

Alg searches for element table and has to return true if it is exists.

private boolean isCompositeExists(Node fieldNode) {
   NodeList childNodes = fieldNode.getChildNodes();
   if (childNodes != null) {
      for (int i = 0; i < childNodes.getLength(); i++) {
         Node child = childNodes.item(i);

         isCompositeExists(child);

         if (child.getNodeName().equals("table")) {
            return true;
         }

      }
   }
   return false;
}
1
  • 6
    Why are you discarding the return value from the recursive call? Commented Feb 11, 2014 at 12:37

1 Answer 1

2

You probably need something like:

private boolean isCompositeExists(Node fieldNode) {
  NodeList childNodes = fieldNode.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
     Node child = childNodes.item(i);
     if (child.getNodeName().equals("table") || isCompositeExists(child)) {
        return true;
     }
  }
   return false;
}

Note that you do not need to check getChildNodes() for null.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.