Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

loading 2D array

Hi there,

I'm trying to learn how to read xml data and loading the data of each element into a 2D array named scheduleData using SAX. I'm having partial success but can't figure out why only part of the data gets loaded into the array. I'm working with a test xml string (below) that consists of three rows of data for column headings named: "DATE","TIME","LOCATION","EVENT","COMMENTS".

When I run the code and print the contents of the 2D array I get:

2005-2-15 | 10:30am. | rm 213 | test 2 | open book |
2005-2-15 | 10:30am. | rm 213 | test 3 | closed book |
null | null | null | null | null |

It should be:

2005-2-15 | 10:30am. | rm 213 | test 1 | |
2005-2-15 | 10:30am. | rm 213 | test 2 | open book |
2005-2-15 | 10:30am. | rm 213 | test 3 | closed book |

Assuming the following xml data:

private String xmlString = "<?xml version=\"1.0\"?><schedule>\r\n<schedule_id>55</schedule_id>\r\n<course_title>Java Programming 1</course_title>\r\n<item_count>3</item_count>\r\n<items>\r\n<item>\r\n<date>2005-2-15</date>\r\n<time>10:30am.</time>\r\n<location>rm 213</location>\r\n<event>test 1</event>\r\n<comments></comments>\r\n</item>\r\n<item>\r\n<date>2005-2-15</date>\r\n<time>10:30am.</time>\r\n<location>rm 213</location>\r\n<event>test 2</event>\r\n<comments>open book</comments>\r\n</item>\r\n<item>\r\n<date>2005-2-15</date>\r\n<time>10:30am.</time>\r\n<location>rm 213</location>\r\n<event>test 3</event>\r\n<comments>closed book</comments>\r\n</item>\r\n</items>\r\n</schedule>";

I call a method with this code:

try{
XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
org.xml.sax.ContentHandler handler = new ScheduleHandler();
parser.setContentHandler(handler);
InputSource source = new InputSource(new StringReader(xmlString));
parser.parse(source);

//PRINT
for(int i = 0; i < scheduleData.length; i++)
{
for(int j = 0; j < scheduleData[i].length; j++)
{
System.out.print(scheduleData[i][j] + " | ");
}
System.out.println();
}

}
catch(SAXException se){se.printStackTrace();}
catch(IOException ioe){ioe.printStackTrace();}

The heart of this program is in the class I'm using that extends DefaultHandler and it looks like this:

class ScheduleHandler extends DefaultHandler
{
boolean haveItemCount = false;
boolean haveDateElement = false;
boolean haveTimeElement = false;
boolean haveLocationElement = false;
boolean haveEventElement = false;
boolean haveCommentsElement = false;

int index = 0;

public void initializeArray(int items)
{
scheduleData = new String[items][5];
}

public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes atts)throws SAXException
{
if(localName.equals(ItemCountElementName))
haveItemCount = true;
else if(localName.equals(DateElementName))
haveDateElement = true;
else if(localName.equals(TimeElementName))
haveTimeElement = true;
else if(localName.equals(LocationElementName))
haveLocationElement = true;
else if(localName.equals(EventElementName))
haveEventElement = true;
else if(localName.equals(CommentsElementName))
haveCommentsElement = true;
}

public void endElement(String namespaceURI, String localName, String qualifiedName)throws SAXException
{
if(localName.equals(ItemCountElementName))
haveItemCount = false;
else if(localName.equals(DateElementName))
haveDateElement = false;
else if(localName.equals(TimeElementName))
haveTimeElement = false;
else if(localName.equals(LocationElementName))
haveLocationElement = false;
else if(localName.equals(EventElementName))
haveEventElement = false;
else if(localName.equals(CommentsElementName))
haveCommentsElement = false;
}

public void characters(char[] text, int start, int length)throws SAXException
{
StringBuffer buffer = new StringBuffer();
try
{
if(haveItemCount)
{
for(int i = start; i < start+length; i++)
{
buffer.append(text[i]);
}

initializeArray(Integer.parseInt(buffer.toString()));
buffer.delete(0, buffer.length());
}
if(haveDateElement)
{
for(int i = start; i < start+length; i++)
{
buffer.append(text[i]);
}
scheduleData[index][0] = buffer.toString();
buffer.delete(0, buffer.length());
}
if(haveTimeElement)
{
for(int i = start; i < start+length; i++)
{
buffer.append(text[i]);
}
scheduleData[index][1] = buffer.toString();
buffer.delete(0, buffer.length());
}
if(haveLocationElement)
{
for(int i = start; i < start+length; i++)
{
buffer.append(text[i]);
}
scheduleData[index][2] = buffer.toString();
buffer.delete(0, buffer.length());
}
if(haveEventElement)
{
for(int i = start; i < start+length; i++)
{
buffer.append(text[i]);
}
scheduleData[index][3] = buffer.toString();
buffer.delete(0, buffer.length());
}
if(haveCommentsElement)
{
for(int i = start; i < start+length; i++)
{
buffer.append(text[i]);
}
scheduleData[index][4] = buffer.toString();
buffer.delete(0, buffer.length());
index++;
}
}
catch(NumberFormatException nfe){nfe.printStackTrace();}
}
}

Please advise,

Alan
[7060 byte] By [ashiers] at [2007-11-11 6:43:42]