finding the location of the current XML node of its kind. - Java XML

Java examples for XML:DOM Node

Description

finding the location of the current XML node of its kind.

Demo Code


//package com.java2s;

import org.w3c.dom.Node;

public class Main {
    /**/*from   w w  w  . j  a va 2s  .c  om*/
     * aims at finding the location of the current node of its kind.
     * the idea is to count how many previous siblings does it have, 
     * @param aNode
     * @return
     */
    public static int getLocHomoSibling(Node aNode) {
        int countOfBranch = 0;
        Node preSibling = aNode;
        while ((preSibling = preSibling.getPreviousSibling()) != null) {
            if (preSibling.getNodeName().equals(aNode.getNodeName())) {
                countOfBranch++;
            }
        }
        return countOfBranch;
    }
}

Related Tutorials