This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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.
public void characters (char ch[], int start, int length)
{
String charString="";
for (int i = start; i < start + length; i++) {
switch (ch[i]) {
case '\\':
Thanks Neal...your solution worked great for me...
Now I would actually like to add these values into a Hash map.
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?
Anjali Raman
Ranch Hand
Joined: Nov 28, 2007
Posts: 57
posted
0
My additional question is - How can I add these into a Hashmap?
Saw something similar in javaranch, but the examples was not clear enough.
Anjali Raman
Ranch Hand
Joined: Nov 28, 2007
Posts: 57
posted
0
Also can you please let me know whats the best way to add this into a Hashmap.
I cant use the nodes as Keys as they will repeat .
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.
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
9
posted
0
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
<?xml version="1.0"?>
<company><staff><firstname>yong</firstname><lastname>mook kim</lastname><nickname>mkyong</nickname><salary>100000</salary></staff></company>
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.
Anjali Raman
Ranch Hand
Joined: Nov 28, 2007
Posts: 57
posted
0
@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.
CLA-0:
KeyValue m02000c0003 4.0499999999999998224
m02000c0004 5.9333333333333335702
.
.
.
.
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.
Anjali Raman
Ranch Hand
Joined: Nov 28, 2007
Posts: 57
posted
0
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.
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12327
1
posted
0
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.