Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList Problem while parsing XML...

 
Joe Budden
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic