Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * @param el an Element
     * @return the ordinal position 1..N of this element amongst its sibling
     * elements (of any name), as a String; 
     * or '0' if the element has no parent
     */
    public static String ordinalPosition(Element el) {
        String position = "0";
        Node parent = el.getParentNode();
        if (parent instanceof Element) {
            int pos = 0;
            NodeList nl = ((Element) parent).getChildNodes();
            for (int i = 0; i < nl.getLength(); i++) {
                Node n = nl.item(i);
                if (n instanceof Element)
                    pos++;
                if (n.equals(el))
                    position = new Integer(pos).toString();
            }
        }
        return position;
    }
}