递归函数中的 if 子句

If clause in a recursive function

我正在尝试构建一个函数,当且仅当节点名称不等于 scriptstyle 时,它才能遍历 Dom 树。这是函数:

      public static void PostOrderTR(Node node) throws XPathExpressionException, MalformedURLException, SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException, IOException, SAXException
{

             if (node == null || node.getNodeName() == null)
                       {
                    return;
                       }
    if(!"script".equals(node.getNodeName())||!"style".equals(node.getLocalName())|| !"style".equals(node.getNodeName()))

             {
                 //do something
                  PostOrderTR(node.getFirstChild());
    }
               if(!"script".equals(node.getNodeName())||!"style".equals(node.getLocalName())|| !"style".equals(node.getNodeName()))

                    PostOrderTR(node.getNextSibling());

         }

但实际上,结果恰恰相反。它遍历所有节点,包括 scriptstyle。我已经尝试用 || 替换 && 并且没有太大变化。

您的条件不成立,因为所有节点名称不是 "script" 或不是 "style",因此所有节点名称都通过。

正确的条件是:

if(!("script".equals(node.getNodeName())||"style".equals(node.getLocalName()))

这意味着节点名称既不是"script"也不是"style"。