Java XML Node Type countNodesBefore(Node node, short nodeType)

Here you can find the source of countNodesBefore(Node node, short nodeType)

Description

Count the DOM nodes of the supplied type (nodeType) before the supplied node, not including the node itself.

License

Open Source License

Parameter

Parameter Description
node Node whose siblings are to be counted.
nodeType The DOM Node type of the siblings to be counted.

Return

The number of siblings of the supplied type before the supplied node.

Declaration

public static int countNodesBefore(Node node, short nodeType) 

Method Source Code


//package com.java2s;
/*//from  w  w  w  . ja  v  a2s.  c o m
 * ePUB Corrector - https://github.com/vysokyj/epub-corrector/
 *
 * Copyright (C) 2012 Jiri Vysoky
 *
 * ePUB Corrector is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * ePUB Corrector is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cobertura; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

import org.w3c.dom.*;

public class Main {
    /**
     * Count the DOM nodes of the supplied type (nodeType) before the supplied
     * node, not including the node itself.
     * <p/>
     * Counts the sibling nodes.
     *
     * @param node     Node whose siblings are to be counted.
     * @param nodeType The DOM {@link Node} type of the siblings to be counted.
     * @return The number of siblings of the supplied type before the supplied node.
     */
    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;
    }

    /**
     * Count the DOM nodes before the supplied node, not including the node itself.
     * <p/>
     * Counts the sibling nodes.
     *
     * @param node Node whose siblings are to be counted.
     * @return The number of siblings before the supplied node.
     */
    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;
    }
}

Related

  1. containsNodeType(org.w3c.dom.Node node, short nodeType)
  2. countNodesBetween(Node node1, Node node2, short nodeType)
  3. getMethodArgValue(final Node typeNode, final String value)
  4. getNodeByType(Node root, int type)
  5. getNodesOfType( NamedNodeMap list, short type)