Best way to store large amounts of data

Greetings!

I have some code that will parse through XML data one character at a time, determine if it's an opening or closing tag, what the tag name is, and what the value between the tags is. All of the results are saved in a 2D string array. Each parent result can have a variable number of child results associated with it and it is possible to have over 2,000 parent results.

Currently, I initialize a new string that I will use to store the values at the beginning of the method.

String[][] initialXMLValues =new String[2000][45]

I have no idea how many results will actually be returned when the method is initially called, so I don't know what to do besides make initialXMLValues around the maximum values I expect to have.

As I parse through the XML, I look for a predefined tag that signifies the start of a result. Each tag/value that follows is stored in a single element of an arraylist in the form "tagname,value". When I reach the closing parent tag, I convert the arraylist to a String[], store the size of the array if it is bigger than the previous array (to track the maximum size of the child results), store it in initialXMLValues[i.] (<- peroid to avoid post showing up in italics), then increment i

When I'm all done parsing, I create a new String String[][] XMLValues =new String[i][j]

, where i is equal to the total number of parent results (from last paragraph) and j is equal to the maximum number of child results. I then use a nested for loop to store all of the values from initialXMLValues into XMLValues. The whole point of this is to minimize the overall size of the returned String Array and minimize the number of null valued fields.

I know this is terribly inefficient, but I don't know a better way to do it. The problem is having to have the size of the array initilized before I really know how many results I'm going to end up storing. Is there a better way to do this?

[2041 byte] By [balfortha] at [2007-11-27 1:21:35]
# 1
Yuk! This looks to be made for SAX XML parsing and it looks to me like you need to create classes to hold associated values. You should use Collections rather than arrays.
sabre150a at 2007-7-11 23:59:29 > top of Java-index,Java Essentials,Java Programming...
# 2

So I'm starting to rewrite my code. I was shocked at how easy it was to implement the SAX parser, but it works great. Now I'm on to doing away with my nasty string arrays.

Of course, the basic layout of the XML is like this:

<result1>

<element1>value</element1>

<element2>value</element2>

</result1>

<result2>

...

I thought about storing each element/value in a HashMap for each result. This works great for a single result. But what if I have 1000 results? Do I store 1000 HashMaps in an ArrayList (if that's even possible)? Is there a way to define an array of HashMaps?

balfortha at 2007-7-11 23:59:29 > top of Java-index,Java Essentials,Java Programming...
# 3
I quote from my previous response "looks to me like you need to create classes to hold associated values" .
sabre150a at 2007-7-11 23:59:29 > top of Java-index,Java Essentials,Java Programming...
# 4

Indeed. I apologize for my ignorance... What I initially attempted to do was create a new class and then create a new instance for each result. But since the number of results are never known, I don't know how to itteratively create some unknown number of uniquely named instances.

Is this what you are recommending? Each result is a seperate object? If this is what you're saying, how do I create new uniquely named instances on the fly?

balfortha at 2007-7-11 23:59:29 > top of Java-index,Java Essentials,Java Programming...