How do I know the nesting level?

How can I find out the nesting level of <node>using DOMxml or any other library? Never had to ask such a question, but now just backed up against the wall.
I would appreciate your help, thanks.

<main>
    <fuel> 
        <filings> 
            <node>
                <node>
                    <node>
                        <node>
                        </node>
                    </node>
                </node>
            </node>
        </filings>
        <thefts> 
            <node> 
                <node>
                    <node>
                            
                    </node>
                </node>
            </node>
        </thefts>
    </fuel>
</main>

Answer 1, authority 100%

So what’s the difficulty? If you have a document DOM tree and a link to a node whose depth you need to find out, then go up the parentNode links until you get to the root and count. Here you will find out the depth.


Answer 2, authority 100%

Like this (pseudocode):

XMLNode n = theNode; //   
int depth = 0;
while(n) {
    n = n.parent;
    ++depth;
}

If you need to search for depth not relative to the root, but relative to some other node, for example thefts, do while(n != thefts).


Answer 3

Just run the loop while there are parents and that’s it.