I want to know a data structure for storing values of tags. My XML structure is :
<personaldetails>
<name>Michael</name>
<age>28</age>
<gender>Male</gender>
<address>123 Streeet 7</address>
<country>India</country>
<pincode> 1234877</pincode>
<contactno>35646445</contactno>
<emailid>michael@abc.com</emailid>
</personaldetails>
I want to store the values at a single place using data structure.Presently i am using setter and getter methods for retrieving values which is making my code large.
Can someone suggest something?
Getters and setters on an object have the advantage of type safety. For example if you tried to write the code
getContactNthen the compiler would give you an error. So although it might make your code long, getters and setters are good.An alternative would be to use a map e.g.
Map<String,String>to store the values. This is more general – you can insert whatever key you like – and is less code, however it doesn’t give you the static analysis benefits of getters and setters. If you mis-spell something you will only discover it when you execute that bit of code – and if it’s in anifthe code might not always get executed and you might miss it.There would also be performance differences between using getters/setters and a map, however, i imagine in 99% of applications they would be irrelevant so it’s not worth pointing out here. If that code becomes a bottleneck you could put timings in to see which approach is better – but do that only if you can verify you have performance problems in this area, otherwise just use whichever technique is more maintainable and easier for you.