# Visit URCap Nodes using the TreeNode.traverse(newProgramVisitor() {}

**URL:** <https://forum.universal-robots.com/t/visit-urcap-nodes-using-the-treenode-traverse-newprogramvisitor/8927>\
**Category:** Java\
**Created:** [September 25, 2020, 2:34pm UTC](https://forum.universal-robots.com/t/visit-urcap-nodes-using-the-treenode-traverse-newprogramvisitor/8927 "2020-09-25T14:34:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon5706138](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon5706138](https://forum.universal-robots.com/u/anon5706138)\
**Post date:** [September 25, 2020, 2:34pm UTC](https://forum.universal-robots.com/t/visit-urcap-nodes-using-the-treenode-traverse-newprogramvisitor/8927/1 "2020-09-25T14:34:03Z")

</div>

Hello,

I have created a few childNodes for a parentNode and I want to visit some of them using this (TreeNode.traverse(new ProgramVisitor() { } )).

I have copied the code of Mr Jacob. Here is below:

TreeNode root = apiProvider.getProgramAPI().getProgramModel().getRootTreeNode(this);

```
// Traverse the program tree, using the ProgramNodeVisitor

root.traverse(new ProgramNodeVisitor() {

    // Override "visit" looking for "URCapProgramNode" nodes

    // Visit will be called for every URCapProgramNode in sub-tree

    @Override

    public void visit(URCapProgramNode programNode, int index, int depth) {

        // Check if the found program node implements the "MyCustomAPI" interface

        if(programNode.canGetAs(MyCustomAPI.class)) {

            // If it did, we found the right node

            // Now we can use "getAs" since we already tested with "canGetAs"

            // After "getAs" we can now call the interface methods 

            programNode.getAs(MyCustomAPI.class).addPoss(poss, firstPoss);

        }

    }

});

```

This gets in every childNode which implements MyCustomAPI. I want to get only in 3rd and 5th childNodes of the same parentNode.

Thanks in Advance,

Mehmet Bilgin

---

<div class="post-metadata">

**Author:** ![anon5706138](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon5706138](https://forum.universal-robots.com/u/anon5706138)\
**Post date:** [September 29, 2020, 7:27am UTC](https://forum.universal-robots.com/t/visit-urcap-nodes-using-the-treenode-traverse-newprogramvisitor/8927/2 "2020-09-29T07:27:08Z")

</div>

TreeNode root = apiProvider.getProgramAPI().getProgramModel().getRootTreeNode(this);

root.traverse(new ProgramNodeVisitor() {

```
@Override

public void visit(URCapProgramNode programNode, int index, int depth) {

    if(programNode.canGetAs(MyCustomAPI.class)) {

	if (index == 2 || index== 4){

        		programNode.getAs(MyCustomAPI.class).addPoss(poss, firstPoss);
	}

    }

}

```

});

Adding if (index == … did the trick for me. Keep in mind that the counting of the siblings starts with zero.

Thanks,
