samedi 28 février 2015

Find the number of nodes at level of x and above



I want to find number the of nodes that are in the same level as the node with @title="d" or are in the higher level


XML:



<item title="a">
<item title="b">
<item title="c"></item>
<item title="d">
<item title="e"></item>
<item title="f"></item>
</item>
</item>
<item title="x">
<item title="y"></item>
<item title="z"></item>
</item>
</item>



L0 . . . . . . a . . . .
/ \
L1 . . . . . b . . x . .
/ \ / \
L2 . . . .c . d .y . z .
/ \
L3 . . . . .e . f . . . .

Some examples:



System.out.println(leftDomainSize("d")); // 7
System.out.println(leftDomainSize("x")); // 3
System.out.println(leftDomainSize("e")); // 9


Code:



public int leftDomainSize(String s){
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(path);
//????
}



Aucun commentaire:

Enregistrer un commentaire