Unformatted text preview:

95-733 Week 5Finding a Pattern using SAXTextMatch.javaPowerPoint PresentationSlide 5Slide 6Slide 7Slide 8Slide 9Slide 10Filtering XMLXMLReaderorg.xml.sax Interface XMLReaderSlide 14Slide 15XMLFilterorg.xml.XMLFilter InterfaceSlide 18Slide 19Slide 20Slide 21org.xml.sax.helpers Class XMLFilterImplSlide 23Some Examples Using FiltersSlide 25Slide 26Filter Demo 2Slide 28Slide 29Slide 30Slide 31Filter Demo 3Slide 33Slide 34Slide 35Slide 36Slide 37Filter Demo 4Slide 39Slide 40Slide 41Filter Demo 5Slide 43Slide 44Slide 45Slide 46Filter Demo 6Slide 48Slide 49Slide 50Slide 51Notes from JDK 1.4 Documentation95-733 Week 5Basic SAX Example From Chapter 5 of XML and JavaWorking with XML SAX Filters as described in Chapter 5Notes from JDK 1.4 DocumentationFinding a Pattern using SAX<?xml version="1.0" encoding="utf-8"?><department> <employee id="J.D"> <name>John Doe</name> <email>[email protected]</email> </employee> <employee id="B.S"> <name>Bob Smith </name> <email>[email protected]</email> </employee></department>department.xmlNotes from JDK 1.4 DocumentationTextMatch.javaimport java.io.IOException;import java.util.Stack;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.helpers.XMLReaderFactory;public class TextMatch extends DefaultHandler { StringBuffer buffer; String pattern; Stack context;Notes from JDK 1.4 Documentation public TextMatch(String pattern) { this.buffer = new StringBuffer(); this.pattern = pattern; this.context = new Stack(); }Notes from JDK 1.4 Documentationprotected void flushText() { if (this.buffer.length() > 0) { String text = new String(this.buffer); if (pattern.equals(text)) { System.out.print("Pattern '"+this.pattern +"' has been found around "); for (int i = 0; i < this.context.size(); i++) { System.out.print("/"+this.context.elementAt(i)); } System.out.println(""); } } this.buffer.setLength(0); }Notes from JDK 1.4 Documentation public void characters(char[] ch, int start, int len) throws SAXException { this.buffer.append(ch, start, len); } public void ignorableWhitespace(char[] ch, int start, int len) throws SAXException { this.buffer.append(ch, start, len); } public void processingInstruction(String target, String data) throws SAXException { // Nothing to do because PI does not affect the meaning // of a document. }Notes from JDK 1.4 Documentation public void startElement(String uri, String local, String qname, Attributes atts) throws SAXException { this.flushText(); this.context.push(local); } public void endElement(String uri, String local, String qname) throws SAXException { this.flushText(); this.context.pop(); }Notes from JDK 1.4 Documentationpublic static void main(String[] argv) { if (argv.length != 2) { System.out.println("TextMatch <pattern> <document>"); System.exit(1); } try { XMLReader xreader = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser"); xreader.setContentHandler(new TextMatch(argv[0])); xreader.parse(argv[1]); } catch (IOException ioe) { ioe.printStackTrace(); } catch (SAXException se) { se.printStackTrace(); } }}The XMLReaderinterface declaressetContentHandler andparse.Notes from JDK 1.4 Documentation<?xml version="1.0" encoding="utf-8"?><department> <employee id="J.D"> <name>John Doe</name> <email>[email protected]</email> </employee> <employee id="B.S"> <name>Bob Smith </name> <email>[email protected]</email> </employee></department>Looking [email protected] from JDK 1.4 DocumentationD:\McCarthy\www\95-733\examples\chap05>java TextMatch "[email protected]" Department.xmlPattern '[email protected]' has been found around /department/employee/emailNotes from JDK 1.4 DocumentationFiltering XMLPerhaps we would like to modify an existing XMLdocument.Or, perhaps we would like to generate and XML documentfrom a flat file or Database.We’ll look at six examples that will make the filtering processclear.Notes from JDK 1.4 DocumentationXMLReaderNotes from JDK 1.4 Documentationorg.xml.sax Interface XMLReaderXMLReader is the interface that an XML parser's SAX2 driver must implement. This interface allows an application to set and query features and properties in the parser, to register event handlers for document processing, and to initiate a document parse.Notes from JDK 1.4 Documentationorg.xml.sax Interface XMLReaderTwo example methods declared in this interface are:voidsetDTDHandler(DTDHandle rhandler) Allow an application to register a DTD event handler.voidparse(InputSource i nput) Parse an XML document.Notes from JDK 1.4 DocumentationXMLReaderXML sourceparsesetContenthandlercontentHandlerCreate XMLReader.Tell it what to parse.Tell it where itscontentHandler is.Tell it to parse.Notes from JDK 1.4 DocumentationXMLFilterNotes from JDK 1.4 Documentationorg.xml.XMLFilter InterfaceAn XML filter is like an XML reader, except that it obtains its events from another XML reader rather than a primary source like an XML document or database. Filters can modify a stream of events as they pass on to the final application.For example, the Filter might set its own contentHandler. Theparser will call that one. This intervening handler can be programmed to call the application’s handler. Thus, the callsfrom the parser to the handler are filtered.Notes from JDK 1.4 DocumentationXMLFilterpackage org.xml.sax;public interface XMLFilter extends XMLReader { // This method allows the application to link // the filter to a parent reader (which may // be another filter). The argument may not be null. public void setParent(XMLReader parent);Notes from JDK 1.4 Documentation // This method allows the application to query the // parent reader (which may be another filter). // It is generally a bad idea to perform any // operations on the parent reader directly: // they should all pass through this filter. public XMLReader getParent();}Notes from JDK 1.4 DocumentationXMLFilter14 MethodsXMLReader Interface14 XMLReader Methods +


View Full Document

CMU ISM 95733 - Basic SAX Example

Download Basic SAX Example
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Basic SAX Example and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Basic SAX Example 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?