Java XML NodeList getCurrentListPosition(Node refNode, NodeList list)

Here you can find the source of getCurrentListPosition(Node refNode, NodeList list)

Description

returns an element's position in the given NodeList

License

BSD License

Parameter

Parameter Description
refNode the element to get the index for
list the NodeList to search

Return

the position starting with 1, or -1 if refNode was null

Declaration

public static int getCurrentListPosition(Node refNode, NodeList list) 

Method Source Code


//package com.java2s;
/*/* w  w w  .  jav a  2s  .c  o  m*/
 * Copyright (c) 2012. betterFORM Project - http://www.betterform.de
 * Licensed under the terms of BSD License
 */

import org.w3c.dom.*;

public class Main {
    /**
     * returns an element's position in the given NodeList
     *
     * @param refNode the element to get the index for
     * @param list    the NodeList to search
     * @return the position starting with 1, or -1 if refNode was null
     */
    public static int getCurrentListPosition(Node refNode, NodeList list) {
        if (refNode == null) {
            return -1;
        }

        int counter = 1;

        for (int n = 0; n < list.getLength(); n++, counter++) {
            if (list.item(n) == refNode) {
                return counter;
            }
        }

        return -1;
    }
}

Related

  1. findNodeByName(NodeList nodeList, String name)
  2. getAllNodes(NodeList nodeList, List nodes)
  3. getArray(NodeList nodeList)
  4. getArray(NodeList nodeList)
  5. getContent(NodeList nl, String path)
  6. getDeepNode(String name[], NodeList nodes)
  7. getDSNameListInScriptText(String scriptText, NodeList tableList)
  8. getElementById(NodeList nodeList, String id)
  9. getElementFromItem(NodeList list, int index)