Java XML Element Sibling countPrecedingSiblingsOfType(Element inElem)

Here you can find the source of countPrecedingSiblingsOfType(Element inElem)

Description

count Preceding Siblings Of Type

License

Common Public License

Parameter

Parameter Description
inElem a parameter

Declaration

private static int countPrecedingSiblingsOfType(Element inElem) 

Method Source Code

//package com.java2s;
//License from project: Common Public License 

import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**//from w  ww .j  av a  2  s.  co m
     * @param inElem
     * @return
     */
    private static int countPrecedingSiblingsOfType(Element inElem) {
        int cnt = 1; // Elements are 1-indexed in XPath
        Node node = inElem.getPreviousSibling();
        String typeName = inElem.getNodeName();
        while (node != null) {
            while (node.getNodeType() != Node.ELEMENT_NODE) {
                node = node.getPreviousSibling();
                if (node == null) {
                    break;
                }
            }
            if (node != null) {
                if (((Element) node).getNodeName().equals(typeName)) {
                    cnt++;
                }
                node = node.getPreviousSibling();
            }
        }
        return cnt;
    }
}

Related

  1. getNextElementSibling(Element aElement)
  2. getNextSibling(Element element)
  3. getNextSibling(Element h2)
  4. getNextSibling(Element node, String name)