Java XML教程(二)__教程 |
|
日期:2007-5-20 1:28:01 人气:183 [大 中 小] |
|
|
|
}
/** Start document. */ public void startDocument() { System.out.println(""); }
/** Start element. */ public void startElement(String name, AttributeList attrs) { System.out.print("<"); System.out.print(name); if (attrs != null) { int len = attrs.getLength(); for (int i = 0; i < len; i++) { System.out.print(" "); System.out.print(attrs.getName(i)); System.out.print("=""); System.out.print(attrs.getValue(i)); System.out.print("""); } } System.out.print(">"); }
/** Characters. */ public void characters(char ch[], int start, int length) { System.out.print(new String(ch, start, length)); }
/** Ignorable whitespace. */ public void ignorableWhitespace(char ch[], int start, int length) { characters(ch, start, length); }
/** End element. */ public void endElement(String name) { System.out.print(""); System.out.print(name); System.out.print(">"); }
/** End document. */ public void endDocument() { // No need to do anything. }
// // ErrorHandler methods //
/** Warning. */ public void warning(SAXParseException ex) { System.err.println("[Warning] "+ getLocationString(ex)+": "+ ex.getMessage()); }
/** Error. */ public void error(SAXParseException ex) { System.err.println("[Error] "+ getLocationString(ex)+": "+ ex.getMessage()); }
/** Fatal error. */ public void fatalError(SAXParseException ex) throws SAXException { System.err.println("[Fatal Error] "+ getLocationString(ex)+": "+ ex.getMessage()); throw ex; }
/** Returns a string of the location. */ private String getLocationString(SAXParseException ex) { StringBuffer str = new StringBuffer();
String systemId = ex.getSystemId(); if (systemId != null) { int index = systemId.lastIndexOf(`/`); if (index != -1) systemId = systemId.substring(index + 1); str.append(systemId); } str.append(`:`); str.append(ex.getLineNumber()); str.append(`:`); str.append(ex.getColumnNumber());
return str.toString(); }
/** Main program entry point. */ public static void main(String argv[]) { if (argv.length == 0) { System.out.println("Usage: java saxOne uri"); System.out.println(" where uri is the URI of your XML document."); System.out.println(" Sample: java saxOne sonnet.xml"); System.exit(1); }
saxOne s1 = new saxOne(); s1.parseURI(argv[0]); } }
saxCounter.java
/* * (C) Copyright IBM Corp. 1999 All rights reserved. * * US Government Users Restricted Rights Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * * The program is provided "as is" without any warranty express or |
|
出处:本站原创 作者:佚名 |
|
|