• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

ArrayList Problem while parsing XML...

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic