95 733 Week 5 Basic SAX Example From

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 <? xml version="1. 0" encoding="utf-8"? > <department> <employee id="J. D"> department. xml <name>John Doe</name> <email>John. Doe@foo. com</email> </employee> <employee id="B. S"> <name>Bob Smith </name> <email>Bob. Smith@foo. com</email> </employee> </department>

Text. Match. java import 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. Default. Handler; import org. xml. sax. helpers. XMLReader. Factory; public class Text. Match extends Default. Handler { String. Buffer buffer; String pattern; Stack context;

public Text. Match(String pattern) { this. buffer = new String. Buffer(); this. pattern = pattern; this. context = new Stack(); }

protected void flush. Text() { 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. element. At(i)); } System. out. println(""); } } this. buffer. set. Length(0); }
![public void characters(char[] ch, int start, int len) throws SAXException { this. buffer. append(ch, public void characters(char[] ch, int start, int len) throws SAXException { this. buffer. append(ch,](http://slidetodoc.com/presentation_image_h2/4edd7851818f0ff476fcbdd5009fa464/image-6.jpg)
public void characters(char[] ch, int start, int len) throws SAXException { this. buffer. append(ch, start, len); } public void ignorable. Whitespace(char[] ch, int start, int len) throws SAXException { this. buffer. append(ch, start, len); } public void processing. Instruction(String target, String data) throws SAXException { // Nothing to do because PI does not affect the meaning // of a document. }

public void start. Element(String uri, String local, String qname, Attributes atts) throws SAXException { this. flush. Text(); this. context. push(local); } public void end. Element(String uri, String local, String qname) throws SAXException { this. flush. Text(); this. context. pop(); }
![public static void main(String[] argv) { if (argv. length != 2) { System. out. public static void main(String[] argv) { if (argv. length != 2) { System. out.](http://slidetodoc.com/presentation_image_h2/4edd7851818f0ff476fcbdd5009fa464/image-8.jpg)
public static void main(String[] argv) { if (argv. length != 2) { System. out. println("Text. Match <pattern> <document>"); System. exit(1); } try { XMLReader xreader = XMLReader. Factory. create. XMLReader( "org. apache. xerces. parsers. SAXParser"); xreader. set. Content. Handler(new Text. Match(argv[0])); xreader. parse(argv[1]); } catch (IOException ioe) { ioe. print. Stack. Trace(); The XMLReader } catch (SAXException se) { interface declares se. print. Stack. Trace(); set. Content. Handler and } parse. } }

<? xml version="1. 0" encoding="utf-8"? > <department> <employee id="J. D"> <name>John Doe</name> <email>John. Doe@foo. com</email> </employee> <employee id="B. S"> <name>Bob Smith </name> <email>Bob. Smith@foo. com</email> </employee> </department> Looking for Bob. Smith@foo. com

D: Mc. Carthywww95 -733exampleschap 05>java Text. Match "Bob. Smith@foo. com" Department. xml Pattern 'Bob. Smith@foo. com' has been found around /department/employee/email

Filtering XML Perhaps we would like to modify an existing XML document. Or, perhaps we would like to generate and XML document from a flat file or Database. We’ll look at six examples that will make the filtering process clear.

XMLReader Notes from JDK 1. 4 Documentation

org. xml. sax Interface XMLReader is the interface that an XML parser's SAX 2 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 Documentation

org. xml. sax Interface XMLReader Two example methods declared in this interface are: voidset. DTDHandler(DTDHandler handler) Allow an application to register a DTD event handler. voidparse(Input. Source input) Parse an XML document. Notes from JDK 1. 4 Documentation

XMLReader XML source parse set. Contenthandler Create XMLReader. Tell it what to parse. Tell it where its content. Handler is. Tell it to parse. content. Handler

XMLFilter 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 content. Handler. The parser will call that one. This intervening handler can be programmed to call the application’s handler. Thus, the calls from the parser to the handler are filtered. 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 set. Parent(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 get. Parent(); } Notes from JDK 1. 4 Documentation

XMLFilter XMLReader Interface 14 Methods XMLFilter Interface 14 XMLReader Methods + 2

XMLFilter XMLReader Object XMLFilter Object All methods of XMLReader are here. They may block, pass on, or modify the calls to the parent

org. xml. sax. helpers Class XMLFilter. Impl All Implemented Interfaces: Content. Handler, DTDHandler, Entity. Resolver, Error. Handler, 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 defined here, in XMLFilter. Impl, for XML content.

org. xml. sax. helpers Class XMLFilter. Impl 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 – XMLFilter. Impl(XMLReader parent) Construct an XML filter with the specified parent. 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. XMLReader. Factory; import org. xml. sax. helpers. Default. Handler; import java. io. IOException; import org. xml. sax. SAXException;
![public class Main. Driver { public static void main(String[] argv) throws SAXException, IOException { public class Main. Driver { public static void main(String[] argv) throws SAXException, IOException {](http://slidetodoc.com/presentation_image_h2/4edd7851818f0ff476fcbdd5009fa464/image-25.jpg)
public class Main. Driver { public static void main(String[] argv) throws SAXException, IOException { // Get a parser XMLReader parser = XMLReader. Factory. create. XMLReader( "org. apache. xerces. parsers. SAXParser"); // Get a handler My. Handler my. Handler = new My. Handler(); // Tell the parser about the handler parser. set. Content. Handler(my. Handler); // Parse the input document parser. parse(argv[0]); } }

class My. Handler extends Default. Handler { // Handle events from the parser public void start. Document() throws SAXException { System. out. println("start. Document is called: "); } public void end. Document() throws SAXException { System. out. println("end. Document is called: "); } } D: Mc. Carthywww95 -733examplesxmlfilter>java Main. Driver department. xml start. Document is called: end. Document is called:

Filter Demo 2 // Filter demon 2 // Adding an XMLFilter. Impl that does nothing but supply // an object that acts as an intermediary. import org. xml. sax. XMLReader; import org. xml. sax. helpers. XMLReader. Factory; import org. xml. sax. helpers. Default. Handler; import org. xml. sax. helpers. XMLFilter. Impl; import java. io. IOException; import org. xml. sax. SAXException;
![public class Main. Driver 2 { public static void main(String[] argv) throws SAXException, IOException public class Main. Driver 2 { public static void main(String[] argv) throws SAXException, IOException](http://slidetodoc.com/presentation_image_h2/4edd7851818f0ff476fcbdd5009fa464/image-28.jpg)
public class Main. Driver 2 { public static void main(String[] argv) throws SAXException, IOException { // Get a parser XMLReader parser = XMLReader. Factory. create. XMLReader( "org. apache. xerces. parsers. SAXParser"); // Get a handler My. Handler my. Handler = new My. Handler();

// Get a filter – and pass a pointer to the parser XMLFilter. Impl my. Filter = new XMLFilter. Impl(parser); // After we create the XMLFilter. Impl, all of the calls we make // on the parser will go through the filter. For example, we will // call set. Content. Handler 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 XMLFilter. Impl about the handler my. Filter. set. Content. Handler(my. Handler); // Parse the input document my. Filter. parse(argv[0]); } }

class My. Handler extends Default. Handler { // Handle events from the parser public void start. Document() throws SAXException { System. out. println("start. Document is called: "); } public void end. Document() throws SAXException { System. out. println("end. Document is called: "); } }

D: Mc. Carthywww95 -733examplesxmlfilter> java Main. Driver 2 department. xml start. Document is called: end. Document is called:

Filter Demo 3 // Filter demon 3 // Adding an XMLFilter. Impl import org. xml. sax. XMLReader; import org. xml. sax. helpers. XMLReader. Factory; import org. xml. sax. helpers. Default. Handler; import org. xml. sax. helpers. XMLFilter. Impl; import java. io. IOException; import org. xml. sax. SAXException;

class My. Cool. Filter. Impl extends XMLFilter. Impl { public My. Cool. Filter. Impl(XMLReader parser) { super(parser); } // There are two start. Document methods in this // class. This one overrides the inherited method. // The inherited method calls the outside // content. Handler. // The parser calls this method, this method calls // the base class method wich calls the outside handler. public void start. Document() throws SAXException { System. out. println("Inside filter"); super. start. Document(); System. out. println("Leaving filter"); }

public void end. Document() throws SAXException { System. out. println("Inside filter"); super. start. Document(); System. out. println("Leaving filter"); } }
![public class Main. Driver 3 { public static void main(String[] argv) throws SAXException, IOException public class Main. Driver 3 { public static void main(String[] argv) throws SAXException, IOException](http://slidetodoc.com/presentation_image_h2/4edd7851818f0ff476fcbdd5009fa464/image-35.jpg)
public class Main. Driver 3 { public static void main(String[] argv) throws SAXException, IOException { // Get a parser XMLReader parser = XMLReader. Factory. create. XMLReader( "org. apache. xerces. parsers. SAXParser"); // Get a handler My. Handler my. Handler = new My. Handler(); // Get a filter that we will treat as a parser XMLFilter. Impl my. Filter = new My. Cool. Filter. Impl(parser);

// Tell the XMLFilter. Impl about the handler my. Filter. set. Content. Handler(my. Handler); // Parse the input document my. Filter. parse(argv[0]); } } class My. Handler extends Default. Handler { // Handle events from the parser public void start. Document() throws SAXException { System. out. println("start. Document is called: "); } public void end. Document() throws SAXException { System. out. println("end. Document is called: "); }

D: Mc. Carthywww95 -733examplesxmlfilter> java Main. Driver 3 department. xml Inside filter start. Document is called: Leaving filter

Filter Demo 4 // Filter demon 4 // Passing xml to an XMLSerializer import org. xml. sax. XMLReader; import org. xml. sax. helpers. XMLReader. Factory; import org. xml. sax. helpers. Default. Handler; import org. xml. sax. helpers. XMLFilter. Impl; import java. io. File. Output. Stream; import java. io. IOException; import org. xml. sax. SAXException; import org. apache. xml. serialize. XMLSerializer; // not standard import org. apache. xml. serialize. Output. Format; // not standard
![public class Main. Driver 4 { public static void main(String[] argv) throws SAXException, IOException public class Main. Driver 4 { public static void main(String[] argv) throws SAXException, IOException](http://slidetodoc.com/presentation_image_h2/4edd7851818f0ff476fcbdd5009fa464/image-39.jpg)
public class Main. Driver 4 { public static void main(String[] argv) throws SAXException, IOException { // Get a parser XMLReader parser = XMLReader. Factory. create. XMLReader( "org. apache. xerces. parsers. SAXParser"); // we need to write to a file File. Output. Stream fos = new File. Output. Stream("Filtered. xml"); // An XMLSerializer can collect SAX events XMLSerializer xml. Writer = new XMLSerializer(fos, null);

// Tell the parser about the handler (XMLSerializer) parser. set. Content. Handler(xml. Writer); // Parse the input document // The parser sends events to the XMLSerializer parser. parse(argv[0]); } }

D: Mc. Carthywww95 -733examplesxmlfilter> java Main. Driver 4 department. xml D: Mc. Carthywww95 -733examplesxmlfilter>type filtered. xml <? xml version="1. 0"? > <department> <employee id="J. D"> <name>John Doe</name> <email>John. Doe@foo. com</email> </employee> <employee id="B. S"> <name>Bob Smith</name> <email>Bob. Smith@foo. com</email> </employee> <employee id="A. M"> <name>Alice Miller</name> <url href="http: //www. foo. com/~amiller/"/> </employee> </department>

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. XMLReader. Factory; import org. xml. sax. helpers. Default. Handler; import org. xml. sax. helpers. XMLFilter. Impl; import java. io. File. Output. Stream; import java. io. IOException; import org. xml. sax. SAXException; import org. apache. xml. serialize. XMLSerializer; // not standard import org. apache. xml. serialize. Output. Format; // not standard
![public class Main. Driver 5 { public static void main(String[] argv) throws SAXException, IOException public class Main. Driver 5 { public static void main(String[] argv) throws SAXException, IOException](http://slidetodoc.com/presentation_image_h2/4edd7851818f0ff476fcbdd5009fa464/image-43.jpg)
public class Main. Driver 5 { public static void main(String[] argv) throws SAXException, IOException { // Get a parser XMLReader parser = XMLReader. Factory. create. XMLReader( "org. apache. xerces. parsers. SAXParser"); // we need to write to a file File. Output. Stream fos = new File. Output. Stream("Filtered. xml"); // An XMLSerializer can collect SAX events XMLSerializer xml. Writer = new XMLSerializer(fos, null); // Get a filter XMLFilter. Impl my. Filter = new Another. Cool. Filter. Impl(parser);

// Tell the XMLFilter. Impl about the handler (XMLSerializer) my. Filter. set. Content. Handler(xml. Writer); // Parse the input document my. Filter. parse(argv[0]); } }

class Another. Cool. Filter. Impl extends XMLFilter. Impl { public Another. Cool. Filter. Impl(XMLReader parser) { super(parser); } public void start. Document() throws SAXException { System. out. println("Inside filter"); super. start. Document(); System. out. println("Leaving filter"); } public void end. Document() throws SAXException { System. out. println("Inside filter"); super. end. Document(); System. out. println("Leaving filter"); } }

D: Mc. Carthywww95 -733examplesxmlfilter> java Main. Driver 5 department. xml Inside filter Leaving filter Filtered. xml is as before.

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. XMLReader. Factory; import org. xml. sax. helpers. Default. Handler; import org. xml. sax. helpers. XMLFilter. Impl; import java. io. File. Output. Stream; import java. io. IOException; import org. xml. sax. SAXException; import org. apache. xml. serialize. XMLSerializer; // not standard import org. apache. xml. serialize. Output. Format; // not standard
![public class Main. Driver 6 { public static void main(String[] argv) throws SAXException, IOException public class Main. Driver 6 { public static void main(String[] argv) throws SAXException, IOException](http://slidetodoc.com/presentation_image_h2/4edd7851818f0ff476fcbdd5009fa464/image-48.jpg)
public class Main. Driver 6 { public static void main(String[] argv) throws SAXException, IOException { // Get a parser XMLReader parser = new My. Cool. Parser(); // we need to write to a file File. Output. Stream fos = new File. Output. Stream("Filtered. xml"); // An XMLSerializer can collect SAX events XMLSerializer xml. Writer = new XMLSerializer(fos, null); // Tell the parser about the handler (XMLSerializer) parser. set. Content. Handler(xml. Writer);

// Parse the input document parser. parse("Some query or file name or. . . "); } } class My. Cool. Parser extends XMLFilter. Impl { public My. Cool. Parser() { }
![public void parse(String a. File. Name. Or. SQLQuery) throws IOException, SAXException { char[] ch public void parse(String a. File. Name. Or. SQLQuery) throws IOException, SAXException { char[] ch](http://slidetodoc.com/presentation_image_h2/4edd7851818f0ff476fcbdd5009fa464/image-50.jpg)
public void parse(String a. File. Name. Or. SQLQuery) 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 start. Document(); start. Element("", "My. New. Tag", "", null); characters(ch, 0, 2); end. Element("", "My. New. Tag", ""); end. Document(); } }

D: Mc. Carthywww95 -733examplesxmlfilter>java Main. Driver 6 D: Mc. Carthywww95 -733examplesxmlfilter>type filtered. xml <? xml version="1. 0"? > <My. New. Tag>Hi</My. New. Tag>
- Slides: 51