import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class mysax extends DefaultHandler{
public static String nows;
public void processingInstruction (String target, String data)
throws SAXException
{
_print("renwu="+target+" shuju="+data);
}
public static void _print(String s)
{
System.out.println(s);
}
public void setDocumentLocator (Locator locator)
{
_print("Hello,my baby SystemId="+locator.getSystemId());
_print("Hello,my baby ColumnNumber="+locator.getColumnNumber());
_print("Hello,my baby LineNumber="+locator.getLineNumber());
}
public void startDocument ()
throws SAXException
{
_print("Hello! I start Document!");
}
public void endDocument ()
throws SAXException
{
_print("Hello! I end Document!");
}
public void startElement (String uri, String localName,
String qName, Attributes attributes)
throws SAXException
{
nows=qName;
_print("--------------------start Element="+qName);
//_print("localName="+localName);
//_print("uri="+uri);
if (attributes!=null)
{
for(int i=0;i<=attributes.getLength()-1;i++)
{
String n=attributes.getQName(i);
_print("sttributes name="+n+" value="+attributes.getValue(i));
}
}
}
public void endElement (String uri, String localName, String qName)
throws SAXException
{
_print("--------------- end Element="+qName);;
}
public void characters (char ch[], int start, int length)
throws SAXException
{
String s=new String(ch,start,length);
if (!s.trim().equals(""))
_print(nows+"----->:"+"value="+s);
}
public static void main(String[] args) {
if (args.length==0)
{
_print("請輸入文件名!");
System.exit(1);
}
else
_print("Hello,My boy,your input is!"+new String(args[0]));
DefaultHandler chulishijian=new mysax();
SAXParserFactory gongchang=SAXParserFactory.newInstance();
try
{
SAXParser jieshiqi=gongchang.newSAXParser();
jieshiqi.parse(new String(args[0]),chulishijian);
}
catch(SAXParseException e)
{
_print("awu error:"+e.getMessage());
_print("error ID="+e.getSystemId());
_print("error Colunm="+e.getColumnNumber());
_print("error line="+e.getLineNumber());
Exception o=e.getException();
_print(o.toString());
}
catch(SAXException e)
{
_print("hahaha error:"+e.getMessage());
}
catch(Throwable t)
{
// t.printStackTrace();
_print("Hello,wo shi awu ya ");
}
//System.exit(0);
throw new myexception();
}
class myexception extends Exception
{
public myexception()
{
super();
}
}
}
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class mysax extends DefaultHandler{
public static String nows;
public void processingInstruction (String target, String data)
throws SAXException
{
_print("renwu="+target+" shuju="+data);
}
public static void _print(String s)
{
System.out.println(s);
}
public void setDocumentLocator (Locator locator)
{
_print("Hello,my baby SystemId="+locator.getSystemId());
_print("Hello,my baby ColumnNumber="+locator.getColumnNumber());
_print("Hello,my baby LineNumber="+locator.getLineNumber());
}
public void startDocument ()
throws SAXException
{
_print("Hello! I start Document!");
}
public void endDocument ()
throws SAXException
{
_print("Hello! I end Document!");
}
public void startElement (String uri, String localName,
String qName, Attributes attributes)
throws SAXException
{
nows=qName;
_print("--------------------start Element="+qName);
//_print("localName="+localName);
//_print("uri="+uri);
if (attributes!=null)
{
for(int i=0;i<=attributes.getLength()-1;i++)
{
String n=attributes.getQName(i);
_print("sttributes name="+n+" value="+attributes.getValue(i));
}
}
}
public void endElement (String uri, String localName, String qName)
throws SAXException
{
_print("--------------- end Element="+qName);;
}
public void characters (char ch[], int start, int length)
throws SAXException
{
String s=new String(ch,start,length);
if (!s.trim().equals(""))
_print(nows+"----->:"+"value="+s);
}
public static void main(String[] args) {
if (args.length==0)
{
_print("請輸入文件名!");
System.exit(1);
}
else
_print("Hello,My boy,your input is!"+new String(args[0]));
DefaultHandler chulishijian=new mysax();
SAXParserFactory gongchang=SAXParserFactory.newInstance();
try
{
SAXParser jieshiqi=gongchang.newSAXParser();
jieshiqi.parse(new String(args[0]),chulishijian);
}
catch(SAXParseException e)
{
_print("awu error:"+e.getMessage());
_print("error ID="+e.getSystemId());
_print("error Colunm="+e.getColumnNumber());
_print("error line="+e.getLineNumber());
Exception o=e.getException();
_print(o.toString());
}
catch(SAXException e)
{
_print("hahaha error:"+e.getMessage());
}
catch(Throwable t)
{
// t.printStackTrace();
_print("Hello,wo shi awu ya ");
}
//System.exit(0);
throw new myexception();
}
class myexception extends Exception
{
public myexception()
{
super();
}
}
}
没有被处理。
编译时应该给你报错了吧,注意看提示