This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes need suggestions on how to deal with data iteration problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "need suggestions on how to deal with data iteration problems" Watch "need suggestions on how to deal with data iteration problems" New topic
Author

need suggestions on how to deal with data iteration problems

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

OK, so I have some xml that looks like this basically:



This data is stored on a web service, and I have the cod written to the point where I can get this data, and then iterate through it in some fashion. Right now I am able to tell my program to go through each project in the project collection, and for each one take the <name text value and add it to a list. Of course I can also do so with projectId, permissions, deices, and channels.

But this leaves me with a bunch of lists loaded up with a bunch of values, and no way to connect them. The end goal is to have a tree for each project here, so it will look like

-Project6
--Device 1
----Channel 2

But right now all I can do with the lsits I have is use the full list of project anmes as the roots, and then the full list of devices as the child. SO I end up witha tree that has roots for each project, and then under each project root every single device is listed, and under each device every single channel is listed, instead of the root project 1 only having the devices and channels that belong to it, etc

I have been experimenting with for loops and such to get this doing what I need, but not much luck.

Any suggestions on how to handle this?>


True wisdom is in knowing you know nothing - Socrates
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Lists of lists.
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Thats funny, I was talking to someone else today and they said the exact same thing and told me to look into the TreeNode type.

If you care to elaborate any please do, I mean i know how to get these values into a list, but Im still a bit mystified by lists.

Is it just a way to store a bunch of string values? Or is there a cooresponding key or type with each item in the list? In other words if I take all the name values from my xml, does the list know that each of these strings it is holding are "names"? And if I then add all the IDs to this same list, does the list know it is holding to types of values? This seems like it would have to be so if I am to use lists to separate the values.

if you can recommend any tutorials or somethign I should look into please do

thanks
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

I'd suggest reading the Javadoc for Collection, List and Map, that should help you on your way, but yes in principle you're going to have to use the first element in a pair as a key,

e.g. List<Y, List><X>> // Y is an object that has a List of X's, Y is also the key that can be searched on.


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Im still having problems with this.

Im not sure how to even start out here.

I know have a better example of what my projects xml looks like:



So as you can see, instead of device Names I am only going to have device Ids under the <devices> tag. This further complicates matters because I am not going to have a project as the root and the device ID under each project root. I know have to figure out a way to Use the project name as the roots, then look at the device IDs found under each project, then go to another xml that holds the deviceId and deviceName together, pull out the correct deviceName by the deviceId that is found in the projects xml, and use that as the node under the projectName root.

And I cant even get the project to have the deviceId as its chld, much less adding in those extra steps!

I have no idea what to do here, I do not even know where to start
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

I think for some reason the forum is adding extra < > to the list stuff you typed there, because it keeps doing it to me too.

So lets use parentheses in place of <> and tell me if this is what you were trying to type there:

List (Y, List(X))

Is that what you meant?

edit - I know Im doing something wrong, but when I try to make a List that has an object and then another list as the parameters it gies me an error saying incorrrect number of parameters
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

you know I may already be part way there without reaizing it. I have four classes pertaining t these two entities. For projects, I have a project class that has all the usual stuff, project name, project Id, etc. ANd then I also have a projectCollection class, that looks like this:




So is this a start? Or will I not even be using this projectCollection class? I also have these two classes for devices btw.

Right now I am able to use for loops, something like for (MeasurementProject measurementProject : measurementProjectCollection.getMeasurementProjects())

which translates to "for every project in the project Collection" right?

So is this on the way to what i am needing.. Right now, when I load the list up with project Names, it is done this way:



So that goes through every project in the collection, grabs the name, and adds it to the projectNameList.

SO am I part way there without realizing it or does this not pertain?

edit - I gotta think this means it is close to what i need. Maybe I just need to use TreeNode here. something like for every project in the collection, TreeNode.addroot(projectname) and TreeNode.addchild(deviceName), or something like that. I dont know this is very abstract to me still, can omeone help me clear this up more please?
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

maybe some ui:repeat tags would be useful here?

This problem is the last one in this project I need to overcome to be able to fully complete this thing

edit - maybe does someone have an example of this being done using xml as the data source? not an actual xml file, but xml that is retrieved as a Stream. SO something like Stream x = method.getResponseBodyAsStream (which returns the xml as a stream)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: need suggestions on how to deal with data iteration problems
 
Similar Threads
really need some help here concerning treeNode data types and the like. (data type issues really)
Using SimpleXml, having issues with nested nesting collection within a collection
Help extracting text out of a response
java.util.ArrayList is not compatible with class java.lang.String exception (SOLVED)
writing java code that will take data from certian xml <elements> and convert into tree nodes