File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes ArrayList Problem while parsing XML... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "ArrayList Problem while parsing XML..." Watch "ArrayList Problem while parsing XML..." New topic

ArrayList Problem while parsing XML...

Joe Budden

Joined: Mar 22, 2006
Posts: 2
Background Information:

I am writing a program that parses a "question/answer" database composed of a XML file. I am using XMLReader to do my parsing. During the startElement function, if the tag is "question" then a new Question object will be pushed onto a stack. However, if it's a simple element just such as the answer then a StringBuffer will be pushed onto the stack. The characters method will append to the StringBuffer and then the SB will be applied to the attribute of the Question object. Lastly, the Question object will be added to an ArrayList. The code works great up until after the 'xr.parse(new InputSource(r));' statement is done parsing the document..

Sample XML file:

Using various debug out statements, the size of the ArrayList is intact throughout the endDocument(), startElement(), etc, methods. However, after the parsing is done, the array list's size is 0. I am completely stumped as to why this is happening. Here is the source:

And the Question object class:

Sorry for the such lengthy thread, but I've tried every single option in the book and I can't find the bug. Please help me. Thank you.

[ June 12, 2006: Message edited by: Joe Budden ]
[ June 12, 2006: Message edited by: Joe Budden ]
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Well, this seems to be rather a common thing to do. Somewhere you create an FAQParser object. So far so good. But inside its parse() method:you create a second FAQParser object. This second FAQParser is the one that does the parsing, since you set it to be the ContentHandler for the XMLReader. But at the end, you disregard it and look in the first FAQParser for results. Naturally there aren't any because it wasn't involved in the parsing.

You only need one FAQParser. So in the parse() method, don't create another one. Just use the one you already have.
I agree. Here's the link:
subject: ArrayList Problem while parsing XML...
It's not a secret anymore!