Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to store xml document in hashtable using sax defaultHandler java

Status
Not open for further replies.

ketandba

Programmer
Nov 15, 2004
38
US
hi,
This is my code.....
************************
public class InvoiceNotesHandler extends DefaultHandler {
private String currentElement = null; // State variable
private String currentQname = null;

String dateString = null;
String descriptionString = null;
String minutesString = null;

StringBuffer thisText = new StringBuffer();

static Hashtable clientNameToRecords;
String clientName = null;

float hours = 0;
ArrayList clientList = new ArrayList();

public void startDocument() {
clientNameToRecords = new Hashtable();
}
public void endDocument() {

}
public void startElement( String uri,
String localname,
String qname,
Attributes attributes)
throws SAXException {

if(qname.equals("client")) {

currentQname = qname;
clientName = attributes.getValue(0);
}

else if(qname.equals("REC")) {

currentQname = qname;
}

currentQname = qname;
}

public void endElement(String uri,
String localname,
String qname)
throws SAXException {
if (thisText.length() > 0) {

if(qname.equals("DATE1"))
{

dateString = thisText.toString().trim();

}
else if(qname.equals("DESCRIPTION"))
{
descriptionString = thisText.toString().trim();

}
else if(qname.equals("MINUTES"))
{
minutesString = thisText.toString().trim();
hours = new Float(minutesString).floatValue();

}else if(qname.equals("REC"))
{

Record record = new Record(dateString, descriptionString, hours);
clientList.add(record);
}
thisText.delete(0, thisText.length());
}

if(qname.equals("client")){

clientNameToRecords.put(clientName, clientList);
System.out.print(clientNameToRecords);
clientList.clear();

}
thisText.delete(0, thisText.length());
}

public void characters(char[] ch,int start,int length)
throws SAXException{

thisText.append(ch, start, length);

}
}
******************************
My input xml file..

<client name="A">
<rec>
<date1>010104</date1>
<desc>aaaaaaaaaaaaaaaa</desc>
<minutes>0.30<minutes>
</rec>
<rec>
<date1>010204</date1>
<desc>bbbbbbbbb</desc>
<minutes>0.50<minutes>
</rec>
</client>
<client name = "B">
<rec>
<date1>010104</date1>
<desc>cccccc</desc>
<minutes>0.70<minutes>
</rec>
<rec>
<date1>010804</date1>
<desc>dddddd</desc>
<minutes>0.30<minutes>
</rec>
<rec>
<date1>010904</date1>
<desc>eeeee</desc>
<minutes>0.25<minutes>
</rec>
</client>
<client name = "C">
<rec>
<date1>011104</date1>
<desc>xxxx</desc>
<minutes>0.70<minutes>
</rec>
<rec>
<date1>010504</date1>
<desc>yyyy</desc>
<minutes>0.60<minutes>
</rec>
<rec>
<date1>010404</date1>
<desc>zzzzz</desc>
<minutes>0.25<minutes>
</rec>
</client>
**********************

i want to print the hashtable contents as

{"A"=[010104aaaaaaa0.30,010204bbbbbbb0.50]}
{"B"=[010104cccccc0.70,010804ddddd0.30,010904eee0.25]}
{"C"=[011104xxxx0.70,010504yyyy0.60,010404zzzz0.25]}

but my code give me the output like this..(WRONG)
{"A"=[010104aaaaaaa0.30,010204bbbbbbb0.50]}

{"B"=[010104cccccc0.70,010804ddddd0.30,010904eee0.25],"A"=[010104cccccc0.70,010804ddddd0.30,010904eee0.25]}

{"B"=[011104xxxx0.70,010504yyyy0.60,010404zzzz0.25],"C"=[011104xxxx0.70,010504yyyy0.60,010404zzzz0.25],"A"=[011104xxxx0.70,010504yyyy0.60,010404zzzz0.25]}

i.e. everytime duplicate key with new data is inserted...
pl. give me advice ...i have spent complete two days for solve this problem....but not succeed....

I am very fresh in sax parser ....
Also let me know the best link for getting the example/description of sax processing...

your help is appriciated.....

ketan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top