You can use a boolean that keeps track of whether the parser is currently inside one of the elements that have text content (firstname, lastname, nickname and salary). If it's not, skip character processing.
You might want to modify this so that you don't create a String object everytime the characters method is called by the parser. The concatenation is also creating another String object. Look into StringBuffer as a Class variable.
Joined: Aug 05, 2005
Anjali Raman wrote:If the start element tag is empty like in case of company and staff, how can I make sure that it does not go to characters?
The characters you get after the empty tags like company and staff are nothing to do with the value of the tag. Notice that you also get an 'empty' characters array with every tag. It is actualy the whitespace at the start of every line. You can verify this by parsing a file where the whole xml is on one line
Other than removing the whitespace from the xml file (not recommended as it would make it very unreadable by humans), your only other option is to ignore it. Notice that this whitespace will always be after a closing tag and characters that represent the value of a tag will always be after an opening tag. So you just need to maintain a flag that indicates whether the last r=tag parsed was an opening or closing one.
If the start element tag is empty like in case of company and staff, how can I make sure that it does not go to characters?
XML Elements can have two types of content: element content and character content. So, in your example file the company and staff elements have element content, not character content. For example, the content of the staff element is firstname, lastname, nickname and salary child elements.
You don't have to make sure that parser will not call the characters method, because there is no character content in the staff or company element.
Keep in mind that your SAX content handler is only receiving events from an XML parser, it is not the actual parser. The code in the parser implementation is making sure that it only sends correct events to the content handler.
The Simple API for XML (SAX) is only a programming API, it is not an XML parser. Apache Xerces is an XML parser.
Joined: Nov 28, 2007
@William Brogden : The whole intention to put it in a hash map is that in my program I want to retrieve the values in this format : I will take the example of the huge xml file i had attached.
KeyValue m02000c0003 4.0499999999999998224
Similarly I need to collect the data for CLA-1 node as well
Is there any way to doing this? And since Hashmap takes only unique values and the tags m02000c0003 repeats in the CLA-1 node also, I tried to concatinate the CLA-0 to it so that the key will be unique.
And once again thanks for all your help
Do help to find one good solution for my problem.
Joined: Nov 28, 2007
Also Joanne: You told me that I should set a boolean flag to make sure that the tag has a ned tag. I dint quite understand this.
Could you please brief more on this.
Author and all-around good cowpoke
Joined: Mar 22, 2000
This sounds to me like you really want at two step process based on a hashmap of hashmaps rather than trying to invent some way to make a single composite key.
The first step would retrieve a hashmap for CLA-O entries which in turn has the key and value entities as per your example.