wood burning stoves*
The moose likes XML and Related Technologies and the fly likes How to store values in a ArrayList from startElement? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to store values in a ArrayList from startElement?" Watch "How to store values in a ArrayList from startElement?" New topic
Author

How to store values in a ArrayList from startElement?

Tariq Ahsan
Ranch Hand

Joined: Nov 03, 2003
Posts: 116
Does anybody have any code samples where I can read an extremely big XML file using strictly SAX and store each of the element values into a vector or preferably an ArrayList? Later I would like to iterate through this vector/list from main or some other class.

Would appreciate a lot for any sort of help!!!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
There is nothing magic about SAX that prevents you from using standard Java techniques. You just have to adjust to the "callback" style of event processing.

You have to write a custom startElement method in your custom handler class - in that method, select the data you want to keep and store it in an ArrayList that is an instance variable of your custom handler class.
When you get the endDocument event, you know the complete document has been processed and your ArrayList has all the data. You can do further processing in the handler or with the Thread that called the parser.
Bill
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to store values in a ArrayList from startElement?