wood burning stoves 2.0*
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
Author

ArrayList Problem while parsing XML...

Joe Budden
Greenhorn

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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.
 
GeeCON Prague 2014
 
subject: ArrayList Problem while parsing XML...