-1

I have been trying to figure this for 1hr30 now and its too confusing for what seems like a really simple problem so I came here to ask.

I send a get request to a 3rd party API to get anime (movie) details as xml. I store these in a class Ann.java that was automatically generated by netbean's JAXB xml bindings from a sample xml response.

Originally the xml file I used returned only a single anime entry so I could use

String output = gt.fetchMovie().getAnime().getName();

Where gt is my web service. This would print the name of the anime to my IDE.

I changed the setup so that the 3rd party API response gives me multiple anime results instead of just one. I now search for "evangelion" and get 3 results.

However, the new schema of the xml and java class means that attributes are stored as lists. E.g. if I want to access an anime's getName it is within this structure:

public class Ann {

protected List<Object> animeOrManga;

public List<Object> getAnimeOrManga() {
        if (animeOrManga == null) {
            animeOrManga = new ArrayList<Object>();
        }
        return this.animeOrManga;
    }

public String getName() {
            return name;
        }

I have been trying to figure out how to return getName but can't find anything that explains what I want to do. I'm not smart enough to just "figure this out" and have been 1hr30 trial and error already.

I know I want to do something like this but guessing the syntax is impossible and everything just goes red, or cannot find symbol, or whatever.

List<Object> = gt.fetchMovie().getAnimeOrManga();
For each (list object)
getName();

Thankyou for reading!

user3474126
  • 63
  • 1
  • 12
  • If I were in doubt about how to do simple things like iteration in a language, my first reaction would be to find a tutorial or a book about the language, not ask a question in a forum. – vanza Apr 15 '15 at 02:42
  • update: it has been explained to me that my autogenerated Ann class needs to be manually modified before i can work with it properly. a fix has been given me to in the form of `code`for (Object item: animeOrManga){ if(item instanceof Anime){ Anime a = (Anime) item; System.out.println(a.getName()); }elseif(item instanceof Manga){ Manga m = (Manga) item; System.out.println(m.getName()); } }`code` i am going to look at making my own custom xml parse to only have to deal with the data i need in a simpler way – user3474126 Apr 15 '15 at 06:04

2 Answers2

0

The pseudocodish you provided can be written in Java as follows:

List<Ann> list = new ArrayList<Ann> // You want the list as specific as possible
list.clone(gt.getAnimeOrManga());
for(Ann listObject : list) {        // For each loop
    listObject.getName();
}

What is used is a for-each loop, which is exactly what is sounds: for each thing in this list do this.

The clone() method takes an arraylist and copies it into the new one. See here: How do I copy the contents of one ArrayList into another?

Community
  • 1
  • 1
MLavrentyev
  • 1,827
  • 2
  • 24
  • 32
  • thanks for your reply. it doesn't seem to me like cloning the already-existing data is the right way. i just want to access it from the class it has been saved into. i posted another description of the problem (with a screenshot!), hopefully more simplified, at http://www.teamliquid.net/forum/general/134491-the-big-programming-thread?page=613#12248 if you care to take another look. thanks so much for trying :) – user3474126 Apr 15 '15 at 03:40
0

how about create a class that has name attribute like

public class Animal {
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

then you can use with

List<Animal> animeOrManga = gt.fetchMovie().getAnimeOrManga();

then you can use with each.

Ge Jun
  • 115
  • 1
  • 6
  • everything gets stored in Ann.java class already, so i should be able to just access it straight from there if I can figure out the syntax... I am still trying but the suggestions haven't worked yet :) – user3474126 Apr 15 '15 at 03:06
  • i think you shall make your ann to change protected List animeOrManga to List animeOrManga and remove the getName() function in ann – Ge Jun Apr 15 '15 at 10:03
  • i didn't understand the implication of what you suggested whatsoever, but some other people mentioned it and i started to understand. this is probably the correct answer, however i went a different route and instead of using the 4000 line auto-generated Ann class, i made a custom xml parser that places my wanted data into a simple custom Ann class which is much easier to use. thankyou everyone. – user3474126 Apr 17 '15 at 18:38