Java XML教程(二)__教程 |
|
日期:2007-5-20 1:28:01 人气:183 [大 中 小] |
|
|
|
* implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */
import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException;
import org.xml.sax.AttributeList; import org.xml.sax.HandlerBase; import org.xml.sax.Parser; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.ParserFactory; import com.ibm.xml.parsers.SAXParser;
/** * saxCounter.java * This sample program calculates statistics for an XML document, * based on the SAX events received. When the parse is complete, * it prints the statistics to standard output. */
public class saxCounter extends HandlerBase { int startDocumentEvents = 0; int endDocumentEvents = 0; int startElementEvents = 0; int endElementEvents = 0; int processingInstructionEvents = 0; int characterEvents = 0; int ignorableWhitespaceEvents = 0; int warningEvents = 0; int errorEvents = 0; int fatalErrorEvents = 0;
public void parseURI(String uri) { SAXParser parser = new SAXParser(); parser.setDocumentHandler(this); parser.setErrorHandler(this); try { parser.parse(uri); } catch (Exception e) { System.err.println(e); }
System.out.println("Document Statistics for " + uri + ":"); System.out.println("===================================="); System.out.println("DocumentHandler Events:"); System.out.println(" startDocument " + startDocumentEvents); System.out.println(" endDocument " + endDocumentEvents); System.out.println(" startElement " + startElementEvents); System.out.println(" endElement " + endElementEvents); System.out.println(" processingInstruction " + processingInstructionEvents); System.out.println(" character " + characterEvents); System.out.println(" ignorableWhitespace " + ignorableWhitespaceEvents); System.out.println("ErrorHandler Events:"); System.out.println(" warning " + warningEvents); System.out.println(" error " + errorEvents); System.out.println(" fatalError " + fatalErrorEvents); System.out.println(" ----------"); int totalEvents = startDocumentEvents + endDocumentEvents + startElementEvents + endElementEvents + processingInstructionEvents + characterEvents + ignorableWhitespaceEvents + warningEvents + errorEvents + fatalErrorEvents; System.out.println("Total: " + |
|
出处:本站原创 作者:佚名 |
|
|