当前位置: 技术问答>java相关
用SAX怎么样检查XML的正确性?
来源: 互联网 发布时间:2015-04-24
本文导语: DOM的我知道,SAX呢?怎么做呢? 最好有个例子 | SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(true); | 参考一下几个类的用法。 import org.xml.sax.SAXException; import org....
DOM的我知道,SAX呢?怎么做呢?
最好有个例子
最好有个例子
|
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(true);
spf.setValidating(true);
|
参考一下几个类的用法。
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;
例程:
class MyErrorHandler implements ErrorHandler{//inner class that implements Interface ErrorHandler
public void warning(SAXParseException exception) throws SAXException{
System.out.println("**Parsing Warning**n"+
" Line: "+
exception.getLineNumber()+"n"+
"URI: "+
exception.getSystemId()+"n"+
"Message: "+
exception.getMessage());
throw new SAXException("Warning encountered");
}
public void error(SAXParseException exception)throws SAXException{
System.out.println("**Parsing Error**n"+
" Line: "+
exception.getLineNumber()+"n"+
"URI: "+
exception.getSystemId()+"n"+
"Message: "+
exception.getMessage());
throw new SAXException("Error encountered");
}
public void fatalError(SAXParseException exception) throws SAXException{
System.out.println("*****Parsing Error*****n"+
" Line: "+
exception.getLineNumber()+"n"+
"URI: "+
exception.getSystemId()+"n"+
"Message: "+
exception.getMessage());
throw new SAXException("Fatal Error encountered");
}
}
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;
例程:
class MyErrorHandler implements ErrorHandler{//inner class that implements Interface ErrorHandler
public void warning(SAXParseException exception) throws SAXException{
System.out.println("**Parsing Warning**n"+
" Line: "+
exception.getLineNumber()+"n"+
"URI: "+
exception.getSystemId()+"n"+
"Message: "+
exception.getMessage());
throw new SAXException("Warning encountered");
}
public void error(SAXParseException exception)throws SAXException{
System.out.println("**Parsing Error**n"+
" Line: "+
exception.getLineNumber()+"n"+
"URI: "+
exception.getSystemId()+"n"+
"Message: "+
exception.getMessage());
throw new SAXException("Error encountered");
}
public void fatalError(SAXParseException exception) throws SAXException{
System.out.println("*****Parsing Error*****n"+
" Line: "+
exception.getLineNumber()+"n"+
"URI: "+
exception.getSystemId()+"n"+
"Message: "+
exception.getMessage());
throw new SAXException("Fatal Error encountered");
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。