我正在尝试构建一个函数,当且仅当 Node 名称不等于 script
和 style
时才遍历 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());
}
但在实践中,结果恰恰相反。它会遍历所有 Node ,包括 script
和 style
。我已经尝试用 ||
替换 &&
,但没有发生太大变化。
最佳答案
您的条件不起作用,因为所有 Node 名称要么不是“脚本”,要么不是“样式”,因此所有 Node 名称都通过。
正确的条件是:
if(!("script".equals(node.getNodeName())||"style".equals(node.getLocalName()))
这意味着 Node 名称既不是“script”也不是“style”。
关于java - 递归函数中的 If 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29294319/