SAXDemo2.java

1/2

examples2/ 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 import import import import import import

javax.xml.parsers.SAXParser; javax.xml.parsers.SAXParserFactory; org.xml.sax.Attributes; org.xml.sax.ContentHandler; org.xml.sax.SAXException; org.xml.sax.helpers.AttributesImpl; org.xml.sax.helpers.DefaultHandler;

/** * Lecture 2’s demonstration of SAX 2.0.2 * (that escapes whitespace characters). * * @author Computer Science E-259 **/ public class SAXDemo2 extends DefaultHandler { /** * Main driver. Expects one command-line argument: * the name of the file to parse. * * @param argv [0] - filename */ public static void main(String [] argv) { // ensure proper usage if (argv.length != 1) { System.out.println("Usage: java SAXDemo filename"); System.exit(1); } // grab filename String input = argv[0]; try { // instantiate a SAX parser SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); // instantiate our little demo handler SAXDemo2 handler = new SAXDemo2(); // parse the file parser.parse(input, handler); } catch (Exception e) { e.printStackTrace(); } }

/** * Report a startElement event. * * @param uri namespace * @param localName name of element, sans namespace

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:

* @param * @param * * @throws */

qName attributes SAXException

name of element, with namespace element’s collection of attributes 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

public void characters(char[] ch, int start, int length) throws SAXException { // store characters in a String object String s = new String(ch, start, length); // escape whitespace characters s = s.replaceAll("\f", "\\\\f"); s = s.replaceAll("\n", "\\n"); s = s.replaceAll("\r", "\\\\r"); s = s.replaceAll("\t", "\\\\t"); // output results 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 {

SAXDemo2.java examples2/ 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: }

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"); }

2/2

SAXDemo.java

1/2

examples2/ 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 import import import import import import

javax.xml.parsers.SAXParser; javax.xml.parsers.SAXParserFactory; org.xml.sax.Attributes; org.xml.sax.ContentHandler; org.xml.sax.SAXException; org.xml.sax.helpers.AttributesImpl; org.xml.sax.helpers.DefaultHandler;

/** * Lecture 2’s demonstration of SAX 2.0.2. * * @author Computer Science E-259 **/ public class SAXDemo extends DefaultHandler { /** * Main driver. Expects one command-line argument: * the name of the file to parse. * * @param argv [0] - filename */ public static void main(String [] argv) { // ensure proper usage if (argv.length != 1) { System.out.println("Usage: java SAXDemo filename"); System.exit(1); } // grab filename String input = argv[0]; try { // instantiate a SAX parser SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); // instantiate our little demo handler SAXDemo handler = new SAXDemo(); // parse the file parser.parse(input, handler); } catch (Exception e) { e.printStackTrace(); } }

/** * Report a startElement event. * * @param uri namespace * @param localName name of element, sans namespace * @param qName name of element, with namespace

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:

* @param * * @throws */

attributes SAXException

element’s collection of attributes 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

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

/** * 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 {

SAXDemo.java examples2/ 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: }

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"); }

2/2

SAXDemo2.java 1/2 - The Open Academy

61: * @param uri namespace. 62: * @param localName name of element, sans namespace. 63: * @param qName name of element, with namespace. 64: * @param attributes element's collection of attributes. 65: *. 66: * @throws SAXException general SAX error or warning. 67: */. 68: 69: public void startElement(String uri, ...

6KB Sizes 1 Downloads 181 Views

Recommend Documents

README.txt 1/2 - The Open Academy
8: In these directories are two client-server pairs, one that ... http://www.ammai.com/modules.php?op=modload&name=Sections&file=index&req=viewarticle.

Computer Science E-75 - The Open Academy
Computer Science E-75. Building Dynamic Websites. Harvard Extension School http://www.cs75.net/. Lecture 9: Ajax. David J. Malan [email protected] ...

AttributeConverter1.xsl 1/2 - The Open Academy
AttributeConverter1.xsl. 1/2 examples5/. 1: 2:

README.txt 1/2 - The Open Academy
27: TaxService port = service.getTaxService();. 28: 29: // Make the actual calls to the three methods. 30: double taxpercent = port.calcTaxRate(21.00, 23.10);. 31:.

Computer Science E-75 - The Open Academy
Harvard Extension School http://www.cs75.net/. Lecture 7: JavaScript ... CDATA[. // put cursor in username field if empty ... else put cursor in password field else.

anchor.svg 1/1 - The Open Academy
9: This is CSCI E-259. ...... 4Y,i2c2Td>E+"ZS1D[5Z+XDmn@md9I^'JbRMH"cJ-'e@on .... 44: .

Computer Science E-75 - The Open Academy
9. PHP + JSON. ▫ json_encode($value) http://us3.php.net/manual/en/function.json-encode.php. ▫ eval(string) http://www.json.org/js.html ...

Computer Science E-1 Spring 2010 Scribe ... - The Open Academy
We'll soon make a tool available via web or e-mail that will allow you ... address bar. http://www.cnn.com:80/ will take us to CNN's homepage as usual. • If you'd ...

Computer Science 75 Fall 2009 Scribe Notes ... - The Open Academy
One disadvan- tage of the JavaScript is that you have no way of logging how users are interacting with your search box because no information is being sent to your server. • Which approach might we want to take if we were developing for a mobile de

The Link - Broadlands Academy
Examinations and Careers Officer. Official ESFA photographs from the National Football ... of information: http://www.nam.ac.uk/ waterloo200/about/. Ms Olsen ...

UPSC recruits National Defence Academy & Naval Academy ...
UPSC recruits National Defence Academy & Naval Academy Examination (II) Advertisement.pdf.pdf. UPSC recruits National Defence Academy & Naval ...

12/04/2015 Open Board Meeting Minutes.pdf
Try one of the apps below to open or edit this item. 12/04/2015 Open Board Meeting Minutes.pdf. 12/04/2015 Open Board Meeting Minutes.pdf. Open. Extract.

Restaurants near the Academy - Groups
East Side [110th to 86th Street]. Sarabeth's [in the Wales Hotel] – American. Paola's [in the Wales Hotel] - Italian. Table d' Horte – French. Le Paris Bistrot ...

Oct 12 Open House Boards v6_ForSurvey.pdf
DEEP BAY. DUNSMUIR. PROPOSED NETWORK. IMPROVEMENTS. BACKGROUND. Area H Boundary. Existing Trails. Roads. Rail Corridor. Water Bodies.

2014-12-09 BAC Open House Presentation.pdf
Try one of the apps below to open or edit this item. 2014-12-09 BAC Open House Presentation.pdf. 2014-12-09 BAC Open House Presentation.pdf. Open.

05 May 12 CASEE 25th anniversary open house.pdf
Page 1 of 1. CASEE 25 Year. Anniversary. Open House. 1:00: Tours and Displays. 2:00: Program. 3:00: Light refreshments and Tours. Open to past and present students, parents,. supporters and the public. 11104 NE, 149th St. Brush Prairie, WA. 360.885.5

Academy for
AIMS-UK Islamic Banking & Finance | Online Certifications | Training & Consultancy. 1. INTRODUCTION TO TAKAFUL. A. Meaning of Takaful. ▫ Takaful comes ...

The Prevention Press - North Star Academy
Rocky Heights Middle School. Mountain Vista High School. Rock Canyon High School. ThunderRidge High School. Castle View High School. DC Oakes.