Meaningless Drivel is fun!
The moose likes XML and Related Technologies and the fly likes SAX Parsing Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "SAX Parsing Problem" Watch "SAX Parsing Problem" New topic

SAX Parsing Problem

Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

My application will parse the XML file given as argument and produces GUI based on the XML tags.In that one i had problem in parsing '&' character.

I got this exception,

After reading W3C documentation here.

I changed '&' in the XML file to & amp;(no whitespace) then i am able to run my application but the GUI which gets generated has one problem

In my XML for creating a menu i will use the following tag

then i can able to get the menu properly but if i change the XML as

then i am able to get the menu with label only others.
Why is this happening?
[ April 09, 2008: Message edited by: Balasubramanian Chandrasekaran ]
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

You are using SAX? Then my guess is you are making the standard error, where you assume that the characters() method always returns you the entire text node all at once. That isn't the case. The parser can break a text node into pieces and call characters() once for each piece. And parsers do that.
Nomi Malo

Joined: Mar 20, 2007
Posts: 20
Hi there, I have to do exactly the same task that you are doing but I am unable to do it as i dont have much experience with parsing. I understand the basics of using SAX parser and how startElement, endElement, and characters are called.

My problem is how do we extract this data and keep it in some meaningful form. Like for example how do i keep track of which menu has which sub menus and then what menuitems are under each submenu. Plus i need to keep track of attributes as well as they will effect runtime menus in GUI. Do I need to use some kind of data structure for this.

Your help with be highly appreciated. Regards, nomi
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

Thanks Paul for pointing that behavior. Now i used a StringBuffer inside characters() method to and append its values and then making its length to 0 at startElement() method and endElement() method. Now it got solved.

Code which i used is

Regarding Nomi Malik,
I will explain you how i handled the situation in my code.I will have separate variable for menuName,menuItemName,subMenuName and subMenuItemName which takes the corresponding values from XML and i used a HashMap to hold all the created menus and menuitems.And while parsing once i see a closing tag in XML i will just create the appropriate component and put it to Frame.After parsing the complete XML file i will have a Frame which holds all the components packed and i will finally call frame.setVisible(true);

This one is my implementation explained in short. If you feel this one is not effective your are welcome to provide feedback.
I agree. Here's the link:
subject: SAX Parsing Problem
It's not a secret anymore!