Hai
I have written following code inorder to parse and print the values with out creating another class.
Its compiling fine but throwing a runtime eroor.
Java.Lang.ClassCastException:
The Code is
-----------
import java.io.*;
import java.util.*;
import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderAdapter;
public class parselineitems extends DefaultHandler
{
private List items;
private List currentItem;
//private Item temp;
StringBuffer accumulator;
static private Writer out;
String localRoot = new String();
int i;
String temp;
public static void main(String args[])
{
try
{
XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.crimson.parser.XMLReaderImpl");
DefaultHandler handler = new parselineitems();
parser.setContentHandler(handler);
parser.parse(args[0]);
}
catch (Exception e)
{
System.err.println(e);
}
}
public void startDocument() throws SAXException
{
accumulator = new StringBuffer();
this.items = new ArrayList();
}
public void startElement(String namespaceURI, String localName,
String qualifiedName, Attributes atts) throws SAXException
{
accumulator.setLength(0);
if(localName.equals("item"))
{
this.currentItem = new ArrayList();
}
else
{
}
}
public void characters(char[] text, int start, int length) throws SAXException
{
accumulator.append(text, start, length);
}
public void endElement(String namespaceURI, String localName,
String qualifiedName) throws SAXException
{
if (localName.equals("item"))
{
// we're done with the item, add it to the list
this.items.add(this.currentItem);
}
else if (localName.equals("part-number"))
{
this.currentItem.add(accumulator);
}
else if (localName.equals("quantity"))
{
this.currentItem.add(accumulator);
}
else if (localName.equals("lpn"))
{
this.currentItem.add(accumulator);
}
else if (localName.equals("reference"))
{
this.currentItem.add(accumulator);
}
}
public void endDocument() throws SAXException
{
for (i=0;i<=items.size();i++)
{
List temp2 = new ArrayList();
temp2 = (ArrayList)items.get(i);
for (i=0;i<=temp2.size();i++)
{
temp = (String)temp2.get(i);
System.out.println(" The value of elements" + temp);
}
System.out.println(" ");
}
}
}
---------------------------------------
can u let me know where i went wrong.
Thanks a lot for all ur help.
I appreciate ur help.
Pooja.