Get XML Node level away from root - Java XML

Java examples for XML:DOM Node Value

Description

Get XML Node level away from root

Demo Code


//package com.java2s;

import org.w3c.dom.Node;

public class Main {
    /**// ww  w .  j a v a  2 s  . co  m
     * in this NQF project, the root node is QualityMeasureDocument
     * @param aNode
     * @param rootNode
     * @return the distance a node to the rootnode
     */
    public static int getHeight(Node aNode, Node rootNode) {
        int height = 0;
        if (aNode == null) {
            return 0;
        }
        while (aNode.getParentNode() != rootNode) {
            aNode = aNode.getParentNode();
            height++;
        }
        return height;
    }
}

Related Tutorials