The moose likes Java in General and the fly likes Does TreeNode hold multiple values? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Does TreeNode hold multiple values? " Watch "Does TreeNode hold multiple values? " New topic

Does TreeNode hold multiple values?

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 484

So lets say I have an xml <tag> that has 5 values - 1, 2 ,3 , 4 , 5.

If I do something like

Will that TreeNode x value now hold all 5 of those values or just the last one?
I know that code is not correct completely but you get my meaning. Will TreeNode x hold all of those values which I will then be able to bind to a recursive tree node adaptor? Or shoudl I use something other than a TreeNode value?

True wisdom is in knowing you know nothing - Socrates
Paul Clapham

Joined: Oct 14, 2005
Posts: 19750

Well, first of all it won't compile because you redeclared a local variable with two different types in the same block. And if that wasn't a problem, you don't assign anything to your TreeNode variable so you'll get a NullPointerException when you try to use it in the next line. And I don't know what this TreeNode class is, but appending a TreeNode to itself is likely to end badly. If the TreeNode class allows it, which it probably shouldn't, then you're going to have a circular reference which, when processed, is likely to produce an infinite loop.

Also I'm not sure what it means for an XML <tag> element to have 5 values. Could you display the actual XML which you're asking about?
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 484

edit - now that I think about it, would addChild() be referring to adding a new xml <tag> or the text node within an existing tag? Like I say, this is incomplete, but one thing at a time
edit 1- maybe I should be using TreeNodeImpl Class rather than TreeNode. I thought the TreeNode class was one of the classes i would find at JBoss but I must have ogtten that somewhere else. SO I guess what this comes down to, is that I need to figure out what class to use that will allow me to append the multiple values that should result from that iterator, which I will then be able to bind to a recursive tree node adaptor

Sorry, like I said I know the code is wrong, I just threw it together to hopefully give an idea of what I meant. It will actually be more like:

Im pretty much declaring a TreeNode value, and then for every device in my device collection, Im trying to add DeviceId to TreeNode x - that is assuming it will hold more than one value. This is from the TreeNode class documentation online:

public void addChild(TreeNode child)
Add a new child node to the end of the list.

So addChild is more accurate than append. But like I said I was just trying to get it posted in time. I guess if there is an addChild method it would suggest TreeNode will hold multiple values though right?

Oh yes and the xml:

Right now when I modify that code from above to say:

It returns
printing attempted iteration thorugh devicecollection: exampleDevice1

If you want a more detailed account of that check this out:

But suffice it to say, I finally have deviceCollection being set to that xml from above like Ive been trying to get it to for days. And now Im needing to loop through deviceColelction and pull out teh text node for each instance of the <deviceId> tag, and then add all those values to a single variable (TreeNode x), which I can then use a gettter for and use tio populate my richfaces tree
I agree. Here's the link:
subject: Does TreeNode hold multiple values?
jQuery in Action, 3rd edition