aspose file tools*
The moose likes Java in General and the fly likes Parsing a hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing a hashtable" Watch "Parsing a hashtable" New topic
Author

Parsing a hashtable

Christopher Frankland
Ranch Hand

Joined: Jan 22, 2005
Posts: 42
Hi,

I am executing a bit of code that returns a bunch of values as a hashtable:



Output:

{departmentScore={Floor=8.1, Pharmacy=7.6, Cashier=7.1}, monthlyScore={August=8.7, July=8.3, November=8.4, June=8.1, October=7.5, April=9.5, May=9.1, March=7.8, January=7.3, February=6.6, September=6.4}, groupScore={Store=7.9, District=7.8, Company=7.9}, attributeScore={Grooming=8.0, Rx=9.8, Line=8.2, Take=6.7, Convenience=9.6, Thank=8.0, Anticipate=7.7, Rx Line=8.8, Greet=7.8}}

I need to somehow divide the above into separate values so I can create a String containing XML elements. i.e:

<set name="Floor" value="8.1">
<set name="Pharmacy" value="7.6">
etc...

The piece I am having difficulty with is how to break down the hashtable into individual pieces so I am put them into my string.

Any help much appreciated!

Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

"Parsing" implies converting something from text into structured data, but it looks to me as if you've already got the data you need, and there's no text involved. It looks like you have a Hashtable whose keys are "departmentScore", "monthlyScore", "groupScore", etc, and the corresponding values are other Hashtables nested inside the big one; the "departmentScore" Hashtable contains keys like "Floor", "Pharmacy", and "Cashier", with values 8.1, 7.6, 7.1. So you can just do something like

StringBuffer buffer = new StringBuffer();
for (Iterator entries = result.getEntries(); entries.hasNext() {
Map.Entry entry = (Map.Entry) entries.next();
String key = (String) entry.getKey();
Hashtable value = (Hashtable) entry.getValue();
for (Iterator entries2 = value.getEntries(); entries2.hasNext() {
Map.Entry entry2 = (Map.Entry) entries2.next();
Object key2= entry2.getKey();
Object value2 = entry2.getValue();
buffer.append("<set name=\"");
buffer.append(key);
buffer.append("\" value=\"");
buffer.append(value);
buffer.append("\"/>\n");
}
}


[Jess in Action][AskingGoodQuestions]
Christopher Frankland
Ranch Hand

Joined: Jan 22, 2005
Posts: 42
Thanks. I will give that a go.

Although, I notice there isn't a getEntries() method on Hashtable?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19792
    
  20

It's called entrySet(), and it returns a Set of Map.Entry objects. You'll need to call iterator() on that set as well.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Thanks, Rob. Why doesn't Firefox have built-in Java API completion?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parsing a hashtable