95-733 week 5

51
Notes from JDK 1.4 Docume ntation 95-733 Week 5 sic SAX Example From Chapter 5 of XML and Java rking with XML SAX Filters as described in Chapter

Upload: julio

Post on 19-Jan-2016

38 views

Category:

Documents


0 download

DESCRIPTION

95-733 Week 5. Basic SAX Example From Chapter 5 of XML and Java Working with XML SAX Filters as described in Chapter 5. Finding a Pattern using SAX. John Doe [email protected] - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 95-733 Week 5

Notes from JDK 1.4 Documentation

95-733 Week 5

Basic SAX Example From Chapter 5 of XML and Java

Working with XML SAX Filters as described in Chapter 5

Page 2: 95-733 Week 5

Notes from JDK 1.4 Documentation

Finding 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.xml

Page 3: 95-733 Week 5

Notes from JDK 1.4 Documentation

TextMatch.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;

Page 4: 95-733 Week 5

Notes from JDK 1.4 Documentation

public TextMatch(String pattern) { this.buffer = new StringBuffer(); this.pattern = pattern; this.context = new Stack(); }

Page 5: 95-733 Week 5

Notes from JDK 1.4 Documentation

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

Page 6: 95-733 Week 5

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. }

Page 7: 95-733 Week 5

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

Page 8: 95-733 Week 5

Notes from JDK 1.4 Documentation

public 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.

Page 9: 95-733 Week 5

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]

Page 10: 95-733 Week 5

Notes from JDK 1.4 Documentation

D:\McCarthy\www\95-733\examples\chap05>java TextMatch "[email protected]" Department.xml

Pattern '[email protected]' has been found around /department/employee/email

Page 11: 95-733 Week 5

Notes from JDK 1.4 Documentation

Filtering XML

Perhaps 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.

Page 12: 95-733 Week 5

Notes from JDK 1.4 Documentation

XMLReader

Page 13: 95-733 Week 5

Notes from JDK 1.4 Documentation

org.xml.sax Interface XMLReader

XMLReader 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.

Page 14: 95-733 Week 5

Notes from JDK 1.4 Documentation

org.xml.sax Interface XMLReader

Two example methods declared in this interface are:

voidsetDTDHandler(DTDHandler handler)           Allow an application to register a DTD event handler.voidparse(InputSource input)           Parse an XML document.

Page 15: 95-733 Week 5

Notes from JDK 1.4 Documentation

XMLReader

XML sourceparse

setContenthandler

contentHandler

Create XMLReader.Tell it what to parse.Tell it where itscontentHandler is.Tell it to parse.

Page 16: 95-733 Week 5

Notes from JDK 1.4 Documentation

XMLFilter

Page 17: 95-733 Week 5

Notes from JDK 1.4 Documentation

org.xml.XMLFilter Interface

An 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.

Page 18: 95-733 Week 5

Notes from JDK 1.4 Documentation

XMLFilter

package 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);

Page 19: 95-733 Week 5

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

Page 20: 95-733 Week 5

Notes from JDK 1.4 Documentation

XMLFilter

14 Methods

XMLReader Interface

14 XMLReader Methods + 2

XMLFilter Interface

Page 21: 95-733 Week 5

Notes from JDK 1.4 Documentation

XMLFilter

XMLReader Object XMLFilter Object

All methods of XMLReader arehere. They mayblock, pass on,or modify the calls to the parent

Page 22: 95-733 Week 5

Notes from JDK 1.4 Documentation

org.xml.sax.helpers Class XMLFilterImpl

All Implemented Interfaces: ContentHandler, DTDHandler, EntityResolver, ErrorHandler, XMLFilter, XMLReader

All XMLReader methods are defined. These methods, by default, pass calls to the parent XMLReader.

By default, the XMLReader is set to call methods definedhere, in XMLFilterImpl, for XML content.

Page 23: 95-733 Week 5

Notes from JDK 1.4 Documentation

org.xml.sax.helpers Class XMLFilterImpl

This class is designed to sit between an XMLReader and the client application's event handlers. By default, it does nothing but pass requests up to the reader and events on to the handlers unmodified, but subclasses can override specific methods to modify the event stream or the configuration requests as they pass through.

A Constructor –

XMLFilterImpl(XMLReader parent)           Construct an XML filter with the specified parent.

Page 24: 95-733 Week 5

Notes from JDK 1.4 Documentation

Some Examples Using Filters

// Filter demon 1

// A very simple SAX program

import org.xml.sax.XMLReader;import org.xml.sax.helpers.XMLReaderFactory;import org.xml.sax.helpers.DefaultHandler;

import java.io.IOException;import org.xml.sax.SAXException;

Page 25: 95-733 Week 5

Notes from JDK 1.4 Documentation

public class MainDriver {

public static void main(String[] argv) throws SAXException, IOException { // Get a parser XMLReader parser = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser"); // Get a handler MyHandler myHandler = new MyHandler();

// Tell the parser about the handler parser.setContentHandler(myHandler);

// Parse the input document parser.parse(argv[0]); }}

Page 26: 95-733 Week 5

Notes from JDK 1.4 Documentation

class MyHandler extends DefaultHandler { // Handle events from the parser

public void startDocument() throws SAXException { System.out.println("startDocument is called:"); }

public void endDocument() throws SAXException { System.out.println("endDocument is called:"); }}D:\McCarthy\www\95-733\examples\xmlfilter>java MainDriver department.xmlstartDocument is called:endDocument is called:

Page 27: 95-733 Week 5

Notes from JDK 1.4 Documentation

Filter Demo 2// Filter demon 2

// Adding an XMLFilterImpl that does nothing but supply// an object that acts as an intermediary.

import org.xml.sax.XMLReader;import org.xml.sax.helpers.XMLReaderFactory;import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.helpers.XMLFilterImpl;

import java.io.IOException;import org.xml.sax.SAXException;

Page 28: 95-733 Week 5

Notes from JDK 1.4 Documentation

public class MainDriver2 {

public static void main(String[] argv) throws SAXException, IOException {

// Get a parser XMLReader parser = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser");

// Get a handler MyHandler myHandler = new MyHandler();

Page 29: 95-733 Week 5

Notes from JDK 1.4 Documentation

// Get a filter – and pass a pointer to the parserXMLFilterImpl myFilter = new XMLFilterImpl(parser);

// After we create the XMLFilterImpl, all of the calls we make // on the parser will go through the filter. For example, we will// call setContentHandler on the filter and not the parser. // When we create the filter (it implements many interfaces), // the parser will call filter methods first. These methods will, // in turn, call our methods. // Tell the XMLFilterImpl about the handlermyFilter.setContentHandler(myHandler);

// Parse the input documentmyFilter.parse(argv[0]); }}

Page 30: 95-733 Week 5

Notes from JDK 1.4 Documentation

class MyHandler extends DefaultHandler { // Handle events from the parser

public void startDocument() throws SAXException { System.out.println("startDocument is called:"); }

public void endDocument() throws SAXException { System.out.println("endDocument is called:"); }

}

Page 31: 95-733 Week 5

Notes from JDK 1.4 Documentation

D:\McCarthy\www\95-733\examples\xmlfilter>

java MainDriver2 department.xmlstartDocument is called:endDocument is called:

Page 32: 95-733 Week 5

Notes from JDK 1.4 Documentation

Filter Demo 3

// Filter demon 3// Adding an XMLFilterImpl

import org.xml.sax.XMLReader;import org.xml.sax.helpers.XMLReaderFactory;import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.helpers.XMLFilterImpl;

import java.io.IOException;import org.xml.sax.SAXException;

Page 33: 95-733 Week 5

Notes from JDK 1.4 Documentation

class MyCoolFilterImpl extends XMLFilterImpl {

public MyCoolFilterImpl(XMLReader parser) { super(parser); } // There are two startDocument methods in this // class. This one overrides the inherited method. // The inherited method calls the outside // contentHandler. // The parser calls this method, this method calls // the base class method wich calls the outside handler. public void startDocument() throws SAXException { System.out.println("Inside filter"); super.startDocument(); System.out.println("Leaving filter"); }

Page 34: 95-733 Week 5

Notes from JDK 1.4 Documentation

public void endDocument() throws SAXException { System.out.println("Inside filter"); super.startDocument(); System.out.println("Leaving filter"); }}

Page 35: 95-733 Week 5

Notes from JDK 1.4 Documentation

public class MainDriver3 {

public static void main(String[] argv) throws SAXException, IOException {

// Get a parser XMLReader parser = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser");

// Get a handler MyHandler myHandler = new MyHandler();

// Get a filter that we will treat as a parser XMLFilterImpl myFilter = new MyCoolFilterImpl(parser);

Page 36: 95-733 Week 5

Notes from JDK 1.4 Documentation

// Tell the XMLFilterImpl about the handler myFilter.setContentHandler(myHandler);

// Parse the input document myFilter.parse(argv[0]); }}

class MyHandler extends DefaultHandler { // Handle events from the parser public void startDocument() throws SAXException { System.out.println("startDocument is called:"); } public void endDocument() throws SAXException { System.out.println("endDocument is called:"); }}

Page 37: 95-733 Week 5

Notes from JDK 1.4 Documentation

D:\McCarthy\www\95-733\examples\xmlfilter>java MainDriver3 department.xmlInside filterstartDocument is called:Leaving filterInside filterstartDocument is called:Leaving filter

Page 38: 95-733 Week 5

Notes from JDK 1.4 Documentation

Filter Demo 4// Filter demon 4

// Passing xml to an XMLSerializer

import org.xml.sax.XMLReader;import org.xml.sax.helpers.XMLReaderFactory;import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.helpers.XMLFilterImpl;import java.io.FileOutputStream;import java.io.IOException;import org.xml.sax.SAXException;import org.apache.xml.serialize.XMLSerializer; // not standardimport org.apache.xml.serialize.OutputFormat; // not standard

Page 39: 95-733 Week 5

Notes from JDK 1.4 Documentation

public class MainDriver4 {

public static void main(String[] argv) throws SAXException, IOException {

// Get a parser XMLReader parser = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser"); // we need to write to a file FileOutputStream fos = new FileOutputStream("Filtered.xml"); // An XMLSerializer can collect SAX events XMLSerializer xmlWriter = new XMLSerializer(fos, null);

Page 40: 95-733 Week 5

Notes from JDK 1.4 Documentation

// Tell the parser about the handler (XMLSerializer) parser.setContentHandler(xmlWriter);

// Parse the input document // The parser sends events to the XMLSerializer parser.parse(argv[0]); }}

Page 41: 95-733 Week 5

Notes from JDK 1.4 Documentation

D:\McCarthy\www\95-733\examples\xmlfilter>java MainDriver4 department.xml

D:\McCarthy\www\95-733\examples\xmlfilter>type filtered.xml<?xml version="1.0"?><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> <employee id="A.M"> <name>Alice Miller</name> <url href="http://www.foo.com/~amiller/"/> </employee> </department>

Page 42: 95-733 Week 5

Notes from JDK 1.4 Documentation

Filter Demo 5// Filter demon 5// Placing a filter between the parser and the// XMLSerializer

import org.xml.sax.XMLReader;import org.xml.sax.helpers.XMLReaderFactory;import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.helpers.XMLFilterImpl;import java.io.FileOutputStream;import java.io.IOException;import org.xml.sax.SAXException;import org.apache.xml.serialize.XMLSerializer; // not standardimport org.apache.xml.serialize.OutputFormat; // not standard

Page 43: 95-733 Week 5

Notes from JDK 1.4 Documentation

public class MainDriver5 {

public static void main(String[] argv) throws SAXException, IOException {

// Get a parser XMLReader parser = XMLReaderFactory.createXMLReader( "org.apache.xerces.parsers.SAXParser"); // we need to write to a file FileOutputStream fos = new FileOutputStream("Filtered.xml"); // An XMLSerializer can collect SAX events XMLSerializer xmlWriter = new XMLSerializer(fos, null);

// Get a filter XMLFilterImpl myFilter = new AnotherCoolFilterImpl(parser);

Page 44: 95-733 Week 5

Notes from JDK 1.4 Documentation

// Tell the XMLFilterImpl about the handler (XMLSerializer) myFilter.setContentHandler(xmlWriter);

// Parse the input document myFilter.parse(argv[0]); }}

Page 45: 95-733 Week 5

Notes from JDK 1.4 Documentation

class AnotherCoolFilterImpl extends XMLFilterImpl {

public AnotherCoolFilterImpl(XMLReader parser) { super(parser); } public void startDocument() throws SAXException { System.out.println("Inside filter"); super.startDocument(); System.out.println("Leaving filter"); } public void endDocument() throws SAXException { System.out.println("Inside filter"); super.endDocument(); System.out.println("Leaving filter"); }}

Page 46: 95-733 Week 5

Notes from JDK 1.4 Documentation

D:\McCarthy\www\95-733\examples\xmlfilter>java MainDriver5 department.xmlInside filterLeaving filterInside filterLeaving filter

Filtered.xml is as before.

Page 47: 95-733 Week 5

Notes from JDK 1.4 Documentation

Filter Demo 6// Filter demo 6

// Writing our own parser and passing calls to a filter

import org.xml.sax.XMLReader;import org.xml.sax.helpers.XMLReaderFactory;import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.helpers.XMLFilterImpl;import java.io.FileOutputStream;import java.io.IOException;import org.xml.sax.SAXException;

import org.apache.xml.serialize.XMLSerializer; // not standardimport org.apache.xml.serialize.OutputFormat; // not standard

Page 48: 95-733 Week 5

Notes from JDK 1.4 Documentation

public class MainDriver6 {

public static void main(String[] argv) throws SAXException, IOException { // Get a parser XMLReader parser = new MyCoolParser(); // we need to write to a file FileOutputStream fos = new FileOutputStream("Filtered.xml"); // An XMLSerializer can collect SAX events XMLSerializer xmlWriter = new XMLSerializer(fos, null); // Tell the parser about the handler (XMLSerializer) parser.setContentHandler(xmlWriter);

Page 49: 95-733 Week 5

Notes from JDK 1.4 Documentation

// Parse the input document parser.parse("Some query or file name or ..."); }}

class MyCoolParser extends XMLFilterImpl {

public MyCoolParser() { }

Page 50: 95-733 Week 5

Notes from JDK 1.4 Documentation

public void parse(String aFileNameOrSQLQuery) throws IOException, SAXException {

char[] ch = new char[10]; ch[0] = 'H'; ch[1] = 'i';

// go to a file or go to a DBMS with a query // make calls to call back methods when this // code feels it's appropriate startDocument(); startElement("", "MyNewTag", "", null); characters(ch, 0, 2); endElement("", "MyNewTag", ""); endDocument(); }}

Page 51: 95-733 Week 5

Notes from JDK 1.4 Documentation

D:\McCarthy\www\95-733\examples\xmlfilter>java MainDriver6

D:\McCarthy\www\95-733\examples\xmlfilter>type filtered.xml<?xml version="1.0"?><MyNewTag>Hi</MyNewTag>