views:

205

answers:

2

Hey,

I am trying to parse xml file using Sax parser. The file has variable number of same elements. I want to count the number of times the element is present. And i want this count before parsing, so that I can declare an array of appropriate size.

One way is to count them in a separately class with another parser (clumsy) and other way is dynamic array (List Array). Is there any other better way to do this?

Also, Is it possible to make an ArrayList of my class..? because I want an array of type myClass.

+2  A: 

Sax basically does not have any history, so you cannot count and then go back and parse again. Why not collect in a list and at the end convert the list to an array?

Note: for complex work on output from Web service a small XSLT program can do wonders.

Thorbjørn Ravn Andersen
A: 

In my opinion, one good option is to create a List:

List elements = new ArrayList();

Then inside the SAX class that handles the XML file, in the method endElement(...) you just need to handle when a tag is ended and then add the element to the List previously created. No need to worry about length then.

elements.add(...);
Fran Roy