I have an XML file and i am using the SAX implementation of the xerces parser to parse the file. I have successfully created the custom object model of the xml file.
i have then created a
servlet the code of which is given below. i am calling this servlet thru an html form. However, it gives an IllegalAccessException.
Pl let me know what could be the reason.
<thecode>
/*
* Author: Jayashree
* Created: 10/01/2001 12:51:33
* Modified: 10/01/2001 12:51:33
*/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.apache.xerces.parsers.SAXParser;
class DocumentSAXParser extends HttpServlet
{
PrintWriter out = null;
public void init(ServletConfig cfg) throws ServletException
{
super.init(cfg);
}
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
out = res.getWriter();
try
{
//InputStreamReader isr = new InputStreamReader(new URL("http://microcell:8080/jayashree/servlets/greeting1.xml")).openStream();
//InputSource is = new InputSource( isr );
//File filename = new File(req.getParameter("text"));
DocSAXHandler handler = new DocSAXHandler();
//DefaultHandler handler = new DefaultHandler();
//SAXParser parser = new SAXParser();
//String parserClassName = "com.sun.xml.parser.Parser";
org.xml.sax.XMLReader parser = new org.apache.xerces.parsers.SAXParser();
org.xml.sax.InputSource input = new InputSource(new FileReader("text1"));
// Full location of the file required
parser.setContentHandler( handler );
parser.parse(input);
//parser.setContentHandler(handler);
//parser.setErrorHandler(handler);
//parser.parse(filename);
}
catch(Exception e)
{
out.println(e.getMessage());
}
}
}
class DocSAXHandler extends DefaultHandler
{
private Document
doc = new Document();
private Greeting g = null;
private Message m = null;
private
String currentElement = null;
public Document getDocument()
{
return(doc);
}
public void startElement (String name, AttributeList atts)
{
String sname = name;
if(sname.equals("greeting"))
{
currentElement = "greeting";
}
else if(sname.equals("message"))
{
currentElement = "message";
}
else if(sname.equals("msgg"))
{
currentElement = "msgg";
}
else if(sname.equals("sendername"))
{
currentElement = "sendername";
}
else if(sname.equals("text"))
{
currentElement = "text";
}
for (int i = 0; i < atts.getLength(); i++)
{
String aname = atts.getName(i);
String type = atts.getType(i);
String value = atts.getValue(i);
}
}
public void endElement(String name)
{
if(name.equals("greeting"))
{
doc.addGreeting(g);
}
else if(name.equals("message"))
{
doc.addMessage(m);
}
}
public void characters( char ch[], int start, int length )
{
String value = new String( ch,start,length);
if(!value.trim().equals(""))
{
if( currentElement.equals("msgg") )
{
m.setMsgg( value );
}
else if( currentElement.equals("sendername") )
{
m.setSenderName( value );
}
else if( currentElement.equals("txt") )
{
m.setText( value );
}
}
}
}
</endof code>
Thanks!!!