95-733 Week 5 Basic SAX Example From Chapter 5 of XML and Java Working with XML SAX Filters as described in Chapter 5.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Web architectures – Laurea Specialistica in Informatica – Università di Trento Java XML parsing.
Advertisements

Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 22. Oktober 2009.
Written by: Dr. JJ Shepherd
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
XML Parsers By Chongbing Liu. XML Parsers  What is a XML parser?  DOM and SAX parser API  Xerces-J parsers overview  Work with XML parsers (example)
Internet Technologies 1 Lecture 1 Introduction.
Summer A-2000, Project Course-- Carnegie Mellon University 1 Financial Engineering Project Course.
1 SAX and more… CS , Spring 2008/9. 2 SAX Parser SAX = Simple API for XML XML is read sequentially When a parsing event happens, the parser invokes.
SAX A parser for XML Documents. XML Parsers What is an XML parser? –Software that reads and parses XML –Passes data to the invoking application –The application.
1 The Simple API for XML (SAX) Part I ©Copyright These slides are based on material from the upcoming book, “XML and Bioinformatics” (Springer-
21-Jun-15 SAX (Abbreviated). 2 XML Parsers SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard.
26-Jun-15 SAX. SAX and DOM SAX and DOM are standards for XML parsers--program APIs to read and interpret XML files DOM is a W3C standard SAX is an ad-hoc.
Internet Technologies1 More XML Schema The main source for these slides is “The XML Companion” by Bradley Other resources:
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection.
28-Jun-15 StAX Streaming API for XML. XML parser comparisons DOM is Memory intensive Read-write Typically used for documents smaller than 10 MB SAX is.
OCT1 Java and XML (DOM and SAX) Some of the material for these slides came from the following sources: “XML a Manager’s Guide” by Kevin Dick “The XML Companion”
Internet Technologies1 Java and XML (DOM and SAX) Some of the material for these slides came from the following sources: “XML a Manager’s Guide” by Kevin.
17 Apr 2002 XML Programming: SAX Andy Clark. SAX Design Premise Generic method of creating XML parser, parsing documents, and receiving document information.
1 Processing XML with Java Dr. Praveen Madiraju Modified from Dr.Sagiv ’ s slides.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
The Joy of SAX (and DOM, and JDOM…) Bill MacCartney 11 October 2004.
Java and C++, The Difference An introduction Unit - 00.
Java Programming Robert Chatley William Lee
SDPL 2003Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
XML for E-commerce II Helena Ahonen-Myka. XML processing model n XML processor is used to read XML documents and provide access to their content and structure.
SAX Parsing Presented by Clifford Lemoine CSC 436 Compiler Design.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
1 Java and XML Modified from presentation by: Barry Burd Drew University Portions © 2002 Hungry Minds, Inc.
SDPL 2002Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How can applications manipulate structured documents? –An overview of document parser.
SDPL 20113: XML APIs and SAX1 3. XML Processor APIs n How can (Java) applications manipulate structured (XML) documents? –An overview of XML processor.
SAX. What is SAX SAX 1.0 was released on May 11, SAX is a common, event-based API for parsing XML documents Primarily a Java API but there implementations.
Beginning XML 4th Edition. Chapter 12: Simple API for XML (SAX)
Extensible MarkUp Language. AGENDA  OVERVIEW OF XML  DATA TYPE DEFINITION LANGUAGE  XML SCHEMA  XML PARSERS 1) DOM PARSER 2) SAX PARSER 3) JAXB PARSER.
Java + XML. Java 2 Enterprise Edition Server Side java Servlets JSP JavaBeans Web Services Database jdbc.
XML Processing in Java. Required tools Sun JDK 1.4, e.g.: JAXP (part of Java Web Services Developer Pack, already in Sun.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Java API for XML Processing (JAXP) Dr. Rebhi S. Baraka Advanced Topics in Information Technology (SICT 4310) Department of Computer.
Sheet 1XML Technology in E-Commerce 2001Lecture 3 XML Technology in E-Commerce Lecture 3 DOM and SAX.
1 Processing XML with Java Modified from Dr.Sagiv ’ s slides.
SDPL Streaming API for XML1 3.4 Streaming API for XML (StAX) n Could we process XML documents more conveniently than with SAX, and yet more efficiently?
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
SNU OOPSLA Lab. DOM/SAX Applications The ubiquitous XML(9) © copyright 2001 SNU OOPSLA Lab.
© Marty Hall, Larry Brown Web core programming 1 Simple API for XML SAX.
XML and SAX (A quick overview) ● What is XML? ● What are SAX and DOM? ● Using SAX.
Java Web 应用开发: J2EE 和 Tomcat 蔡 剑, Ph.D.. 本讲内容 Web 层技术 (III) Custom Tags JSP and XML JSTL.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
SDPL 20063: XML Processor Interfaces1 3. XML Processor APIs n How can (Java) applications manipulate structured (XML) documents? –An overview of XML processor.
Simple API for XML (SAX) Aug’10 – Dec ’10. Introduction to SAX Simple API for XML or SAX was developed as a standardized way to parse an XML document.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
7-Mar-16 Simple API XML.  SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files  DOM is a W3C standard  SAX is an.
SDPL 2001Notes 3: XML Processor Interfaces1 3. XML Processor APIs n How applications can manipulate structured documents? –An overview of document parser.
1 Introduction SAX. Objectives 2  Simple API for XML  Parsing an XML Document  Parsing Contents  Parsing Attributes  Processing Instructions  Skipped.
Java API for XML Processing
Simple API for XML SAX. Agenda l Introduction to SAX l Installation and setup l Steps for SAX parsing l Defining a content handler l Examples Printing.
Parsing with SAX using Java Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Java Programming Language
Accessing Files in Java
Testing and Exceptions
XML Parsers By Chongbing Liu.
Jagdish Gangolly State University of New York at Albany
Java API for XML Processing
A parser for XML Documents
Exception Handling Contents
SAX2 29-Jul-19.
Presentation transcript:

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 Bob Smith department.xml

TextMatch.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.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; public class TextMatch extends DefaultHandler { StringBuffer buffer; String pattern; Stack context;

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

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

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

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

public static void main(String[] argv) { if (argv.length != 2) { System.out.println("TextMatch "); 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 XMLReader interface declares setContentHandler and parse.

John Doe Bob Smith Looking for

D:\McCarthy\www\95-733\examples\chap05>java TextMatch Department.xml Pattern has been found around /department/employee/

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.

Notes from JDK 1.4 Documentation XMLReader

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.

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.setDTDHandlerDTDHandler voidparse(InputSource input) Parse an XML document.parseInputSource

XMLReader XML source parse setContenthandler contentHandler Create XMLReader. Tell it what to parse. Tell it where its contentHandler is. Tell it to parse.

Notes from JDK 1.4 Documentation 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 contentHandler. 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 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(); }

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

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 XMLFilterImpl All Implemented Interfaces: ContentHandlerContentHandler, DTDHandler, EntityResolver, ErrorHandler,DTDHandlerEntityResolverErrorHandler XMLFilterXMLFilter, XMLReaderXMLReader 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 XMLFilterImpl, for XML content.

Notes from JDK 1.4 Documentation org.xml.sax.helpers Class XMLFilterImpl This class is designed to sit between an XMLReader and the XMLReader 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 – XMLFilterImplXMLFilterImpl(XMLReader parent) Construct an XML filter with the specified parent.XMLReader

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;

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

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.xml startDocument is called: endDocument is called:

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;

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

// Get a filter – and pass a pointer to the parser XMLFilterImpl 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 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:"); }

D:\McCarthy\www\95-733\examples\xmlfilter> java MainDriver2 department.xml startDocument is called: endDocument is called:

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;

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

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

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

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

D:\McCarthy\www\95-733\examples\xmlfilter> java MainDriver3 department.xml Inside filter startDocument is called: Leaving filter Inside filter startDocument 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.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 standard import org.apache.xml.serialize.OutputFormat; // not standard

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

// 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]); }

D:\McCarthy\www\95-733\examples\xmlfilter> java MainDriver4 department.xml D:\McCarthy\www\95-733\examples\xmlfilter>type filtered.xml John Doe Bob Smith Alice Miller

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 standard import org.apache.xml.serialize.OutputFormat; // not standard

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

// Tell the XMLFilterImpl about the handler (XMLSerializer) myFilter.setContentHandler(xmlWriter); // Parse the input document myFilter.parse(argv[0]); }

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

D:\McCarthy\www\95-733\examples\xmlfilter> java MainDriver5 department.xml Inside filter Leaving filter 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.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 standard import org.apache.xml.serialize.OutputFormat; // not standard

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

// Parse the input document parser.parse("Some query or file name or..."); } class MyCoolParser extends XMLFilterImpl { public MyCoolParser() { }

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

D:\McCarthy\www\95-733\examples\xmlfilter>java MainDriver6 D:\McCarthy\www\95-733\examples\xmlfilter>type filtered.xml Hi