Java XML Node Previous countElementsBefore(Node node, String tagName)

Here you can find the source of countElementsBefore(Node node, String tagName)

Description

Count the DOM element nodes before the supplied node, having the specified tag name, not including the node itself.

License

Open Source License

Parameter

Parameter Description
node Node whose element siblings are to be counted.
tagName The tag name of the sibling elements to be counted.

Return

The number of siblings elements before the supplied node with the specified tag name.

Declaration

public static int countElementsBefore(Node node, String tagName) 

Method Source Code


//package com.java2s;
/*//  www.  j a  v  a  2  s .c  om
 * 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 element nodes before the supplied node, having the specified
     * tag name, not including the node itself.
     * <p/>
     * Counts the sibling nodes.
     *
     * @param node    Node whose element siblings are to be counted.
     * @param tagName The tag name of the sibling elements to be counted.
     * @return The number of siblings elements before the supplied node with the
     *         specified tag name.
     */
    public static int countElementsBefore(Node node, String tagName) {
        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() == Node.ELEMENT_NODE && ((Element) sibling).getTagName().equals(tagName)) {
                count++;
            }
        }

        return count;
    }
}

Related

  1. countElementsBefore(Node node, String tagName)
  2. getAncesters(Node node)
  3. getAncestor(Node node, String ancestorName)
  4. getAncestorNode(Node visualNode, String tagName)
  5. getAncestors(Node node)