Java 怎么计算所提供的同级节点之间所提供类型(nodeType)的DOM节点数,不包括节点本身

2019年7月23日22:57:05Java 怎么计算所提供的同级节点之间所提供类型(nodeType)的DOM节点数,不包括节点本身已关闭评论 400 2306字阅读7分41秒

1.简介

Java 怎么计算所提供的同级节点之间所提供类型(nodeType)的DOM节点数,不包括节点本身?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import org.w3c.dom.*;

public class Main {
    
    public static int countNodesBetween(Node node1, Node node2,
            short nodeType) {
        Node parent1 = node1.getParentNode();
        if (parent1 == null) {
            System.out.println("Cannot count nodes between [" + node1
                    + "] and [" + node2 + "]. [" + node1
                    + "] has no parent.");
            return 0;
        }

        Node parent2 = node2.getParentNode();
        if (parent2 == null) {
            System.out.println("Cannot count nodes between [" + node1
                    + "] and [" + node2 + "]. [" + node2
                    + "] has no parent.");
            return 0;
        }

        if (parent1 != parent2) {
            System.out.println("Cannot count nodes between [" + node1
                    + "] and [" + node2
                    + "]. These nodes do not share the same sparent.");
            return 0;
        }

        int countBeforeNode1 = countNodesBefore(node1, nodeType);
        int countBeforeNode2 = countNodesBefore(node2, nodeType);
        int count = countBeforeNode2 - countBeforeNode1;

        if (node1.getNodeType() == nodeType) {
            count--;
        }

        return count;
    }

    
    public static int countNodesBetween(Node node1, Node node2) {
        Node parent1 = node1.getParentNode();
        if (parent1 == null) {
            System.out.println("Cannot count nodes between [" + node1
                    + "] and [" + node2 + "]. [" + node1
                    + "] has no parent.");
            return 0;
        }

        Node parent2 = node2.getParentNode();
        if (parent2 == null) {
            System.out.println("Cannot count nodes between [" + node1
                    + "] and [" + node2 + "]. [" + node2
                    + "] has no parent.");
            return 0;
        }

        if (parent1 != parent2) {
            System.out.println("Cannot count nodes between [" + node1
                    + "] and [" + node2
                    + "]. These nodes do not share the same sparent.");
            return 0;
        }

        int countBeforeNode1 = countNodesBefore(node1);
        int countBeforeNode2 = countNodesBefore(node2);
        int count = countBeforeNode2 - countBeforeNode1 - 1;

        return count;
    }

    
    public static int countNodesBefore(Node node, short nodeType) {
        Node parent = node.getParentNode();
        if (parent == null) {
            System.out.println("Cannot count nodes before [" + node
                    + "]. [" + node + "] has no parent.");
            return 0;
        }

        NodeList siblings = parent.getChildNodes();
        int count = 0;
        int siblingCount = siblings.getLength();

        for (int i = 0; i < siblingCount; i++) {
            Node sibling = siblings.item(i);

            if (sibling == node) {
                break;
            }
            if (sibling.getNodeType() == nodeType) {
                count++;
            }
        }

        return count;
    }

    
    public static int countNodesBefore(Node node) {
        Node parent = node.getParentNode();
        if (parent == null) {
            System.out.println("Cannot count nodes before [" + node
                    + "]. [" + node + "] has no parent.");
            return 0;
        }

        NodeList siblings = parent.getChildNodes();
        int count = 0;
        int siblingCount = siblings.getLength();

        for (int i = 0; i < siblingCount; i++) {
            Node sibling = siblings.item(i);

            if (sibling == node) {
                break;
            }
            count++;
        }

        return count;
    }
}

 
继续阅读