foo.xml examples10/ 1: 2: 4: this 5: is a 6: test 7:

1/1

foo.xsd examples10/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:

1/1

SAXValidator3.java

1/2

examples10/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62:

import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException;

/** * Lecture 10’s demonstration of validation * by XML DTD or XML Schema. Just like Lecture 9’s, * except that SAX events are reported, whitespace * characters are displayed as "\n", "\t", and "\r" * explicitly, and line numbers are reported in errors. * * @author Computer Science E-259 **/ public class SAXValidator3 extends DefaultHandler { /** * Main driver. Expects one command-line argument: the name of the * XML file to validate * * @param argv [0] - filename */ public static void main(String [] argv) { if (argv.length == 0) { System.out.println("Usage: SAXValidator3 file [dtd|xsd]"); System.exit(1); } // grab filename String input = argv[0]; // grab validation mechanism, if any String validator = (argv.length > 1) ? argv[1] : null; try { // instantiate a reference to a SAX parser SAXParser parser = null; // instantiate a SAX parser factory SAXParserFactory factory = SAXParserFactory.newInstance(); // instantiate a SAX parser, enabling validation as requested if (validator != null && validator.equals("dtd")) { factory.setValidating(true); parser = factory.newSAXParser(); System.out.println("Validation by DTD on."); } else if (validator != null && validator.equals("xsd")) { factory.setNamespaceAware(true); factory.setValidating(true);

63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124:

parser = factory.newSAXParser(); parser.setProperty ( "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema" ); System.out.println("Validation by XML Schema on."); } else { factory.setValidating(false); parser = factory.newSAXParser(); System.out.println("Validation off."); } // instantiate our little handler SAXValidator3 handler = new SAXValidator3(); // parse the file parser.parse(input, handler); } catch (Exception e) { e.printStackTrace(); } } /** * Report a * * @param * @param * @param * @param * * @throws */

startElement event. uri namespace localName name of element, sans namespace qName name of element, with namespace attributes element’s collection of attributes SAXException

general SAX error or warning

public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { System.out.print("startElement(\"" + qName + ", {"); for (int i = 0; i < atts.getLength(); i++) { System.out.print("(\"" + atts.getQName(i) + "\", \"" + atts.getValue(i) + "\")"); if (i != atts.getLength() - 1) System.out.print(", "); } System.out.println("});"); }

/** * Report a * * @param * @param * @param * * @throws */

characters event. ch start length

characters start position in the character array number of characters to use from the character array

SAXException

general SAX error or warning

SAXValidator3.java

2/2

examples10/ 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186:

187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210:

public void characters(char[] ch, int start, int length) throws SAXException { String s = new String(ch, start, length); s = s.replaceAll("\n", "\\\\n"); s = s.replaceAll("\t", "\\\\t"); s = s.replaceAll("\r", "\\\\r"); System.out.println("characters(\"" + s + "\");"); }

/** * Report an endElement event. * * @param uri namespace * @param localName name of element, sans namespace * @param qName name of element, with namespace * * @throws SAXException general SAX error or warning */ public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("endElement(\"" + qName + "\");"); }

/** * Report a startDocument event. */ public void startDocument() throws SAXException { System.out.println("\nstartDocument();"); }

/** * Report an endDocument event. * * @throws SAXException general SAX error or warning */ public void endDocument() throws SAXException { System.out.println("endDocument();\n"); }

/** * Receive notification of a parser warning. * * @param e the exception thrown */ public void warning (SAXParseException e) { System.out.println("Parsing warning at line " + e.getLineNumber() + ": " + e.getMessage()); }

/** * Receive notification of a recoverable parser error. * * @param e the exception thrown */ public void error (SAXParseException e) { System.out.println("Parsing error at line " + e.getLineNumber() + ": " + e.getMessage()); }

/** * Report a fatal XML parsing error. * * @param e the exception thrown */ public void fatalError (SAXParseException e) { System.out.println("Fatal parsing error at line " + e.getLineNumber()

+ 211: 212: 213: 214: }

": System.exit(1); }

" + e.getMessage());

foo.xml 1/1 - Fas Harvard

15: * characters are displayed as "\n", "\t", and "\r". 16: * explicitly, and line numbers are reported in errors. 17: *. 18: * @author Computer Science E-259. 19: **/.

9KB Sizes 1 Downloads 225 Views

Recommend Documents

anchor.svg 1/1 - Fas Harvard
anchor.svg. 1/1 examples6/svg/. 1: 2:

books.xml 1/1 - Fas Harvard
2: . 3: DOCTYPE emails SYSTEM "emails.dtd">. 4: . 5:

README.txt 1/2 - Fas Harvard
3: Computer Science E-259. 4: 5: 6: OVERVIEW. 7: 8: In these directories are ... 13: * @author Computer Science E-259. 14: **/. 15: 16: public class TaxClient.

DiagServer.java 1/2 - Fas Harvard
DiagServer.java. 2/2 examples7/. 46: byteCount++;. 47: System.out.write(b); ... 2:

XML Schema (Second Edition) - Fas Harvard
After the release of XML 1.0, DTDs were soon recognized as insufficient. ▫ Work towards new schema standards began in early 1998. ▫ Different companies all ...

AttributeConverter1.xsl 1/2 - Fas Harvard
2/2 examples5/. 46: . 47: .... 2/2 examples5/. 44: 45: . 46: 47: . 48:  ...

Computer Science E-259 - Fas Harvard
“SVG is a language for describing two-dimensional graphics in XML. SVG allows ... Adapted from http://www.adobe.com/svg/basics/getstarted2.html. Viewable at ...

Computer Science E-259 - Fas Harvard
Computer Science E-259. XML with Java, Java ... Computer. Laptop. PDA. Web Server. JSP/servlet. JSP/servlet. EJB Server. EJB. EJB. DB. XML? ... Page 10 ...

Computer Science E-259 - Fas Harvard
Apr 13, 2005 - 1. Copyright © 2007, David J. Malan . All Rights Reserved. Computer Science E-259. XML with Java. Lecture 11:.

XML Schema (Second Edition) - Fas Harvard
Establish a contract with trading partners. ▫ Documentation. ▫ Augmentation of instance with default values. ▫ Storage of application information ...

Computer Science E-259 - Fas Harvard
Cut through the hype and get to the value. ▫ Focus on. ▫ practicality: what you need to know to do real work. ▫ applications: what are the tools and technologies.

Computer Science E-259 - Fas Harvard
Computer Science E-259. XML with Java. Lecture 5: XPath 1.0 (and 2.0) and XSLT ... All Rights Reserved. Computer Science E-259. Last Time. ▫ CSS Level 2.

Computer Science E-259 - Fas Harvard
Page 2 .... . . .... unidirectional hyperlinks of today's HTML, as well as.

XPath 1.0 (and 2.0) - Fas Harvard
Computer Science E-259. This Time. ▫ CSS Level 2. ▫ XPath 1.0 (and 2.0). ▫ XSLT 1.0 (and 2.0). ▫ TrAX. ▫ Project 2 .... Displaying XML data on the Web as HTML.

Computer Science E-259 - Fas Harvard
All Rights Reserved. Computer Science E-259. XML with Java. Lecture 2: XML 1.1 and SAX 2.0.2. 24 September 2007. David J. Malan [email protected] ...

Horario FAS Docente.pdf
TALLER DE LENGUA. Y COMUNICACIÓN I. Código 0103011. Agustín Prado. SEMINARIO DE. PENSAMIENTO LÓGICO. MATEMÁTICO. Código 0207010.

Horario FAS Docente.pdf
Rosa Chavarría. DISEÑO DE. VESTUARIO. Código 0306051. Aurora Ayala. METODOLOGÍA DE. LA DANZA III. Código 0308053. Manuel Stagnaro. KINESIOLOGÍA II. Código 0317052. Moises Del Castillo. PSICOLOGÍA. GENERAL. Código 0201050. Doris Ramírez. HOR

2017 FAS Paper Form.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. 2017 FAS Paper ...

ESTATUTO FAS 2013.pdf
Page 1 of 8. Page 1 of 8. Page 2 of 8. Page 2 of 8. Page 3 of 8. Page 3 of 8. ESTATUTO FAS 2013.pdf. ESTATUTO FAS 2013.pdf. Open. Extract. Open with.

Ciclo preparación FAS-extenso.pdf
Whoops! There was a problem loading more pages. Retrying... Ciclo preparación FAS-extenso.pdf. Ciclo preparación FAS-extenso.pdf. Open. Extract. Open with.

Fas-induced Pulmonary Apoptosis and Inflammation ...
respiratory distress syndrome (ARDS)/ALI affects about 7% of all intensive ...... system mediates epithelial injury, but not pulmonary host defenses, in response to ...