参考一下几个类的用法。
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");
}
}
解决方案 »
- 急急急!HibernateTemplate的update锁数据表问题
- 跪求一个正则表达式匹配的问题~~急!!!!
- 现在外面哪种数据库用的多??
- JavaMail的Transport.send(msg)或Transport.connect
- 精华贴:中央认证服务器目录树的算法采集
- 50分求解eclipse+hibernate 为什么运行总提示 Attribute "package" must be declared for element type "hibernate-mapping".错误
- Struts中表单验证出错后如何重新显示表单数据
- Struts:请问怎样按<yyyy-MM-dd>的格式显示ActionForm中的日期格式,(求救 :一直未解决)
- 菜鸟问题~~~怎样实现数据在两个applet中的传输??
- jboss+ejb-ql问题
- ejb的问题
- weblogic & jbuilder 集成开发ejb的问题
spf.setValidating(true);
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;import java.util.*;
import java.io.*;/**
* Program to count the number of "tags" AKA elements in an XML document.
* This example also shows one way to turn on validation and how to use a
* SAX ErrorHandler.
*
* Notes: DefaultHandler is a SAX helper class that implements the SAX
* ContentHandler interface by providing no-op methods. This class
* overrides some of the methods by extending DefaultHandler.
*
*/
public class SAXTagCount extends DefaultHandler {
// A Hashtable with tag names as keys and Integers as values
private Hashtable tags; // Parser calls this once at the beginning of a document
public void startDocument() throws SAXException {
tags = new Hashtable();
} // Parser calls this for each element in a document
public void startElement(String namespaceURI, String localName,
String rawName, Attributes atts)
throws SAXException
{
String key = localName;
Object value = tags.get(key);
if (value == null) {
// Add a new entry
tags.put(key, new Integer(1));
} else {
// Get the current count and increment it
int count = ((Integer)value).intValue();
count++;
tags.put(key, new Integer(count));
}
} // Parser calls this once after parsing a document
public void endDocument() throws SAXException {
Enumeration e = tags.keys();
while (e.hasMoreElements()) {
String tag = (String)e.nextElement();
int count = ((Integer)tags.get(tag)).intValue();
System.out.println("Tag <" + tag + "> occurs " + count
+ " times");
}
} /**
* Convert from a filename to a file URL.
*/
private static String convertToFileURL(String filename) {
// On JDK 1.2 and later, simplify this to:
// "path = file.toURL().toString()".
String path = new File(filename).getAbsolutePath();
if (File.separatorChar != '/') {
path = path.replace(File.separatorChar, '/');
}
if (!path.startsWith("/")) {
path = "/" + path;
}
return "file:" + path;
} private static void usage() {
System.err.println("Usage: SAXTagCount [-v] <filename>");
System.err.println(" -v = validation");
System.exit(1);
} static public void main(String[] args) {
String filename = null;
boolean validation = false; // Parse arguments
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-v")) {
validation = true;
} else {
filename = args[i]; // Must be last arg
if (i != args.length - 1) {
usage();
}
}
}
if (filename == null) {
usage();
} // There are several ways to parse a document using SAX and JAXP.
// We show one approach here. The first step is to bootstrap a
// parser. There are two ways: one is to use only the SAX API, the
// other is to use the JAXP utility classes in the
// javax.xml.parsers package. We use the second approach here
// because it allows the application to use a platform default
// implementation without having to specify a system property.
// After bootstrapping a parser/XMLReader, there are several ways
// to begin a parse. In this example, we use the SAX API. // Create a JAXP SAXParserFactory and configure it
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(validation); XMLReader xmlReader = null;
try {
// Create a JAXP SAXParser
SAXParser saxParser = spf.newSAXParser(); // Get the encapsulated SAX XMLReader
xmlReader = saxParser.getXMLReader();
} catch (Exception ex) {
System.err.println(ex);
System.exit(1);
} // Set the ContentHandler of the XMLReader
xmlReader.setContentHandler(new SAXTagCount()); // Set an ErrorHandler before parsing
xmlReader.setErrorHandler(new MyErrorHandler(System.err)); try {
// Tell the XMLReader to parse the XML document
xmlReader.parse(convertToFileURL(filename));
} catch (SAXException se) {
System.err.println(se.getMessage());
System.exit(1);
} catch (IOException ioe) {
System.err.println(ioe);
System.exit(1);
}
} // Error handler to report errors and warnings
private static class MyErrorHandler implements ErrorHandler {
/** Error handler output goes here */
private PrintStream out; MyErrorHandler(PrintStream out) {
this.out = out;
} /**
* Returns a string describing parse exception details
*/
private String getParseExceptionInfo(SAXParseException spe) {
String systemId = spe.getSystemId();
if (systemId == null) {
systemId = "null";
}
String info = "URI=" + systemId +
" Line=" + spe.getLineNumber() +
": " + spe.getMessage();
return info;
} // The following methods are standard SAX ErrorHandler methods.
// See SAX documentation for more info. public void warning(SAXParseException spe) throws SAXException {
out.println("Warning: " + getParseExceptionInfo(spe));
}
public void error(SAXParseException spe) throws SAXException {
String message = "Error: " + getParseExceptionInfo(spe);
throw new SAXException(message);
} public void fatalError(SAXParseException spe) throws SAXException {
String message = "Fatal Error: " + getParseExceptionInfo(spe);
throw new SAXException(message);
}
}
}
http://java.sun.com/xml/downloads/javaxmlpack.html
文件在examples\SAXTagCount下