i m unable to parse XML document by SAX in
applet but when i run it on my dos prompt it is working fine, actually i m parsing the file but it is not onvoking its startDocument(), endDocument .... functions can anybody solve my problem ?
CODE:
package XML;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.image.*;
import java.util.*;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.*;
import org.w3c.dom.*;
import java.io.*;
public class treeBuilder extends Applet implements DocumentHandler {
int i=20,j=20;
String msg;
Graphics myRef;
//Construct the applet
public treeBuilder() {
}
//Initialize the applet
public void init() {
this.setBackground(Color.white);
msg="it is working";
}// init ends here
public void start (){
/*
try {
parsing();
}
catch(Exception e)
{
msg= "problem in calling " + this.getDocumentBase();
repaint();
}
*/
try{
String uri="" + this.getCodeBase() + "slideSample.xml" ;
Parser parser;
SAXParserFactory spf =SAXParserFactory.newInstance();
SAXParser sp =spf.newSAXParser();
parser =sp.getParser();
parser.setDocumentHandler(new treeBuilder());
parser.setErrorHandler(new MyErrorHandler());
// FileReader fr=new FileReader( uri );
//InputSource is=new InputSource(fr);
parser.parse(uri);
msg= "Mesage displayed if parsing succesfully";
repaint();
}
catch (SAXParseException f){
// System.out.println("Parsing Error");
// System.out.println("at line "+ f.getLineNumber() + " uri " + f.getSystemId());
// System.out.println(f.getMessage());
msg= "Parser Exception";
repaint();
}
catch (SAXException e) {
// e.printStackTrace();
msg= "SAX Exception";
repaint();
}
catch (IOException i){
msg= "IO Exception";
repaint();
}
catch (Throwable t){
// t.printStackTrace();
msg= "Throwable exception " + this.getCodeBase();
repaint();
}
}// start function ends
public void paint(Graphics g){
//g.drawString();
//myRef=g;
j=j+i;
g.drawString(msg,i,j);
//update(g);
}
/* public void update(Graphics g){
//update(g);
this.setBackground(Color.white);
myRef=g;
}
*/
/* space for update function
*/
// public void repaint()
public void stop(){
} // stop functon ends
public void destroy(){
}// destroy ends here
static class MyErrorHandler extends HandlerBase {
// treat validation errors as fatal
public void error (SAXParseException e)
throws SAXParseException
{
throw e;
}
// dump warnings too
public void warning (SAXParseException err)
throws SAXParseException
{
// System.out.println ("** Warning"
//+ ", line " + err.getLineNumber ()
//+ ", uri " + err.getSystemId ());
// System.out.println(" " + err.getMessage ());
}
} // Handler Base ends
//private Writer out;
public void setDocumentLocator(Locator l){
// nothing to do
}
public void startDocument ()
throws SAXException
{
// try {
// out = new OutputStreamWriter (System.out, "UTF8");
// emit ("<?xml version='1.0' encoding='UTF-8'?>\n");
// myRef.drawString("Starting thre document",10,10);
msg= "Starting thre document";
repaint();
//} catch (IOException e) {
// throw new SAXException ("I/O error", e);
//}
}
public void endDocument ()
throws SAXException
{
//try {
//out.write ('\n');
//out.flush ();
//out = null;
//myRef.drawString("Endinging thre document",10,40);
msg= "Starting thre document";
repaint();
// } catch (IOException e) {
// throw new SAXException ("I/O error", e);
// }
}
public void startElement (String tag, AttributeList attrs)
throws SAXException
{
//myRef.drawString("Starting thre document",100,200);
// repaint();
msg= "Starting thre element";
repaint();
/* emit ("<");
emit (tag);
if (attrs != null) {
for (int i = 0; i < attrs.getLength (); i++) {
emit (" ");
emit (attrs.getName (i));
emit ("\"");
// XXX this doesn't quote '&', '<', and '"' in the
// way it should ... needs to scan the value and
// emit '&', '<', and '"' respectively
emit (attrs.getValue (i));
emit ("\"");
}
}
emit (">");
*/
}
public void endElement (String name)
throws SAXException
{
//myRef.drawString("endinging thre document",100,100);
// repaint();
msg= "ending thre document";
repaint();
// emit ("</");
// emit (name);
// emit (">");
}
public void characters (char buf [], int offset, int len)
throws SAXException
{
// NOTE: this doesn't escape '&' and '<', but it should
// do so else the output isn't well formed XML. to do this
// right, scan the buffer and write '&' and '<' as
// appropriate.
// try {
// out.write (buf, offset, len);
//myRef.drawString("Starting thre attributes",10,60);
// repaint();
msg= "Starting thre attributes";
repaint();
// } catch (IOException e) {
// throw new SAXException ("I/O error", e);
// }
}
public void ignorableWhitespace (char buf [], int offset, int len)
throws SAXException
{
// this whitespace ignorable ... so we ignore it!
// this callback won't be used consistently by all parsers,
// unless they read the whole DTD. Validating parsers will
// use it, and currently most SAX nonvalidating ones will
// also; but nonvalidating parsers might hardly use it,
// depending on the DTD structure.
}
public void processingInstruction (String target, String data)
throws SAXException
{
//emit ("<?");
//emit (target);
//emit (" ");
//emit (data);
//emit ("?>");
}
// helpers ... wrap I/O exceptions in SAX exceptions, to
// suit handler signature requirements
private void emit (String s)
throws SAXException
{
// try {
// out.write (s);
// out.flush ();
// } catch (IOException e) {
// throw new SAXException ("I/O error", e);
// }
}