aspose file tools*
The moose likes Beginning Java and the fly likes Data storage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Data storage" Watch "Data storage" New topic
Author

Data storage

James Gibbs
Greenhorn

Joined: Feb 21, 2013
Posts: 29

Hi

I'm still a novice java programmer, been learning it to build Android apps.

I'm building an app which will have two spinner drop down boxes, first one displays categories, second one sub categories. Users choice with the first spinner then displays the sub categories of the main catagory.

Now these categories/sub categories never change, will be read only, so would the best way of storing them be in XML? Each catagory/subcatagory has a name and an ID. There's only a few hundred in total. I'll have to manually enter the data in the xml, what's the easiest way?

Because I don't need to modify/update the categories/sub categories programmatically is the simplest way just to then use a xml parser and adapter, then link the spinners to the adapter? I don't need to use any other ways to store them such arrays/objects?

Thanks

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

James Gibbs wrote:Now these categories/sub categories never change, will be read only, so would the best way of storing them be in XML?

I'm not sure I understand the question.

The best way to store them in Java would be in Category and SubCategory objects - possibly even enums if they're truly static.

What you seem to be asking is "what's the best way to persist them?" - and the answer to that (as it almost invariably is) is: it depends.

It depends what you're going to use them for, how frequently they get updated (if at all), whether they're likely to be needed by any other application...
the parameters are endless.
Personally I'm not a huge fan of XML; I prefer databases - but that probably just makes me an old fart. The only rational reason I have for choosing a database is that there's plainly a relationship between your categories and subcategories; and relationships are what databases are all about.

The fact is that if they truly are static, and don't need to be used by anything outside Java, then an enum (and/or maybe a couple of interfaces) may be the simplest solution of all. After all, you'll probably have to define some classes to handle them anyway.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
James Gibbs
Greenhorn

Joined: Feb 21, 2013
Posts: 29

Thanks for getting back to me Winston.

They are truly static. It's mearly a list of amazon categories names and ids, and the sub categories (names and ids) for each main catagory. Surely a database seems like a lot of overhead for this as they aren't going to be changing and only need be read once at startup? You're right though, there is a relationship and this is what I'm having trouble with. I've built simple arrays which bind to the spinners before, but im not sure how to implement the relationship in a data structure internally.


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

James Gibbs wrote:They are truly static. It's mearly a list of amazon categories names and ids, and the sub categories (names and ids) for each main catagory.
Surely a database seems like a lot of overhead for this as they aren't going to be changing and only need be read once at startup?

Hmmm. Doesn't sound so static to me. Surely Amazon could change those categories any time they like, and/or the subcategories they have?

You're right though, there is a relationship and this is what I'm having trouble with. I've built simple arrays which bind to the spinners before, but im not sure how to implement the relationship in a data structure internally.

OK, well first up: Forget about Spinners.

They have absolutely nothing to do with what you're trying to capture - category data - they're simply a tool for displaying it.

Presumably each of these things have a lot in common (eg, a Name, a URL, maybe some description...), so I'd start out by defining a couple of interfaces: Category and SubCategory, that define what you're going to do with them (which will probably be a bunch of "getter" methods to begin with; and, for Category, a way of getting ALL of its subcategories at once).

If you're going to read them from an XML file, you'll also need some way of converting that XML to your Category and SubCategory objects (and possibly write them back out); but I'd keep that as separate as possible from your base classes - the reason being that if you decide later on to store them in a database, all you need to do is rewrite a bunch of "converters".

Again, if this truly is static information, you could simply bung all that info into a Category enum; but if you've got 3,000 of them it might be a bit unwieldly.

Sorry if this isn't specific enough for you, but there are still a lot of unknowns (at least for me).

Winston
James Gibbs
Greenhorn

Joined: Feb 21, 2013
Posts: 29

Hmmm. Doesn't sound so static to me. Surely Amazon could change those categories any time they like, and/or the subcategories they have?


Possibly, they do provide an API to get the new catagories/sub catagories, but the app i'm making is mostly just for practice. As long as it works for a short peroid of time I'll be happy.

Presumably each of these things have a lot in common (eg, a Name, a URL, maybe some description...), so I'd start out by defining a couple of interfaces: Category and SubCategory, that define what you're going to do with them (which will probably be a bunch of "getter" methods to begin with; and, for Category, a way of getting ALL of its subcategories at once).


This maybe a silly question but why use interfaces? What's their advantage over just making SubCategory a subclass of Catagory?

Again, if this truly is static information, you could simply bung all that info into a Category enum; but if you've got 3,000 of them it might be a bit unwieldly.


There's 400 catagories/sub-catagories in total. Do you think enums could be the way to go?


Here's what I've come up with so far:



SubCatagory will extend Catagory. I'll then create a List of catagory objects which will be used for the spinner adapter. Does this sound like i'm on the right track?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

James Gibbs wrote:There's 400 catagories/sub-catagories in total. Do you think enums could be the way to go?

No. 4 - yes, 40 - possibly; but not 400.

SubCatagory will extend Catagory. I'll then create a List of catagory objects which will be used for the spinner adapter. Does this sound like i'm on the right track?

Pretty much. I suspect you may want to use a Set or Map rather than a List, unless subcategories can be repeated within a category.

But you've got the general idea: start small, and build gradually.

Winston
James Gibbs
Greenhorn

Joined: Feb 21, 2013
Posts: 29

Ok i'm getting a little stuck with the xml parser. I've modified a sample xml parser implementation to work with my xml file. It iterates through each element and returns the name + id for each, which I can then use to build each catagory/sub-catagory object.

However from what I can tell there's no way of telling any difference between a parent and child element, so I won't be able to distinguish what are the catagories (the root elements) and which are the sub catagories (the child elements). I'm using xmlpullparser and from what I can tell there isn't anything built into the library to enable this.



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data storage