In my project i need to load aml data from internet and after that parse it. I have no problem in parsing xml. Each xml has 20 elements. let me explain my question with a sample.
this is the format of xml:
<XML>
<Item1>
<element1>a</element1>
<element2>b</element2>
...
<element20>f</element20>
</Item1>
<Item2>
<element1>d</element1>
<element2>s</element2>
...
<element20>l</element20>
</Item2>
...
</XML>
My question is about the class that i want to create. One way is creating class and put each element as its properties such as:
class myItems {
private String element1;
private String element2;
...
private String element20;
//and also adding all setters and getters
}
Therefore, each item of XML will have one object of this class. So, if i have 100 items in xml file, i will have 100 objects of this class.
second way is using ArrayList in the class. something like:
Class myItems {
ArrayList<String> element1 = new ArrayList<String>();
ArrayList<String> element2 = new ArrayList<String>();
...
ArrayList<String> element20 = new ArrayList<String>();
//and for setters and getters
public ArrayList<String> getElement1() {
return element1;
}
public void setElement1(String str) {
this.element1.add(str);
}
...
}
Therefore, I have one object that it has all of my variables.
My question is, which of these ways is better in terms of speed, memory consumption and anything else?
The best way to do this in my opinion is have a clear model of the object you’re trying to represent, as a class:
You will want to abstract meaningless stuff from the XML structure into your class. For example, you could combine two or three elements from the XML into one member if it makes sense. You can do that when creating the object. You will end up with a smaller footprint (fewer public methods) and a clearer implementation. Then, you can represent the list of items in your program as a
List:Personally, I believe that if you’re not facing serious performance constraints from the beginning (which I don’t think you are), clarity and clean implementation trumps performance. You should be more careful about design as opposed to performance right now. Premature optimization is generally viewed as a bad thing.
Unrelated, here is an interesting presentation on Java memory consumption :).