17 Apr 2002 XML Programming: SAX Andy Clark. SAX Design Premise Generic method of creating XML parser, parsing documents, and receiving document information.

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.
Networking.  Many applications provide & use data & services via the Internet  Android includes multiple networking support classes, e.g.,  java.net.
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)
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.
XML Robert Grimm New York University. The Whirlwind So Far  HTTP  Persistent connections  (Style sheets)  Fast servers  Event driven architectures.
1 The Simple API for XML (SAX) Part I ©Copyright These slides are based on material from the upcoming book, “XML and Bioinformatics” (Springer-
XML Technologies and Applications Rajshekhar Sunderraman Department of Computer Science Georgia State University Atlanta, GA 30302
Parsing XML into programming languages JAXP, DOM, SAX, JDOM/DOM4J, Xerces, Xalan, JAXB.
Xerces The Apache XML Project Yvonne Yao. Introduction Set of libraries that provides functionalities to parse XML documents Set of libraries that provides.
Week 5 Basic SAX Example From Chapter 5 of XML and Java Working with XML SAX Filters as described in Chapter 5.
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.
1 Processing XML with Java Representation and Management of Data on the Internet.
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.
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 Technologies 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.
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
Processing XML Part II Parser Operations with DOM and SAX overview XML Validation with examples Processing XML with SAX (locally and on the internet)
1 Processing XML with Java Dr. Praveen Madiraju Modified from Dr.Sagiv ’ s slides.
Processing of structured documents Spring 2003, Part 5 Helena Ahonen-Myka.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools Leonidas Fegaras.
17 Apr 2002 XML Programming: JAXP Andy Clark. Java API for XML Processing Standard Java API for loading, creating, accessing, and transforming XML documents.
17 Apr 2002 XML Schema Andy Clark. What is it? A grammar definition language – Like DTDs but better Uses XML syntax – Defined by W3C Primary features.
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.
5 Processing XML Parsing XML documents  Document Object Model (DOM)  Simple API for XML (SAX) Class generation Overview.
17 Apr 2002 XML Programming - DOM Andy Clark. DOM Design Premise Derived from browser document model Defined in IDL – Lowest common denominator programming.
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.
EXtensible Markup Language (XML) James Atlas July 15, 2008.
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)
XML Processing in Java. Required tools Sun JDK 1.4, e.g.: JAXP (part of Java Web Services Developer Pack, already in Sun.
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?
J2EE —— 第 5 章 Simple API for XML(SAX). 何时使用 SAX SAX :状态无关 (StAX :状态相关 ) 事件驱动模型,不能倒退到文档前面部分 SAX 与 DOM :相同的错误处理、处理验证错误 何时使用 SAX 把现有数据转换成 XML 速度快,效率高 何时使用.
CSE 6331 © Leonidas Fegaras XML Tools1 XML Tools.
SNU OOPSLA Lab. DOM/SAX Applications The ubiquitous XML(9) © copyright 2001 SNU OOPSLA Lab.
WIRED Detector Description in XML Mark Dönszelmann, Applications for Physics and Infrastructure, IT, CERN XML Detector Description Workshop CERN, 14 April,
Apache DOM Parser©zwzOctober 24, 2002 Wenzhong Zhao Department of Computer Science The University of Kentucky.
© 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.
When we create.rtf document apart from saving the actual info the tool saves additional info like start of a paragraph, bold, size of the font.. Etc. This.
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
1 Processing XML with Java Representation and Management of Data on the Internet.
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.
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.
{ XML Technologies } BY: DR. M’HAMED MATAOUI
XML Parsers By Chongbing Liu.
Jagdish Gangolly State University of New York at Albany
Jagdish Gangolly State University of New York at Albany
Java API for XML Processing
A parser for XML Documents
XML Programming in Java
SAX2 29-Jul-19.
Presentation transcript:

17 Apr 2002 XML Programming: SAX Andy Clark

SAX Design Premise Generic method of creating XML parser, parsing documents, and receiving document information “Streaming” information set – Application registers handlers – Parser “pushes” information to handlers Serial (“as you see it”) notification – Application only uses information it needs

org.xml.sax.XMLReader (1 of 2) Settings – void setFeature(String featureId, boolean state); – boolean getFeature(String featureId); – void setProperty(String propertyId, Object value); – Object getProperty(String propertyId); Parsing – void parse(String systemId); – void parse(InputSource inputSource);

org.xml.sax.XMLReader (2 of 2) Handlers – void setContentHandler(ContentHandler handler); – ContentHandler getContentHandler(); – void setDTDHandler(DTDHandler handler); – DTDHandler getDTDHandler(); – void setErrorHandler(ErrorHandler handler); – ErrorHandler getErrorHandler(); Entity resolver – void setEntityResolver(EntityResolver resolver); – EntityResolver getEntityResolver();

Creating a SAX Parser org.xml.sax.helpers.XMLReaderFactory – XMLReader createXMLReader(); Creates default SAX parser Search method – System property: “org.xml.sax.driver” – Jar Services: “META-INF/services/org.xml.sax.driver” – Distribution specific fallback – XMLReader createXMLReader(String className); Creates SAX parser by name

Parsing a Document Instantiate parser – XMLReader reader = XMLReaderFactory.createXMLReader(); Configure settings – reader.setFeature(“ spaces”, true); – reader.setFeature(“ tion”, false); Parse document – reader.parse(“document.xml”);

org.xml.sax.ContentHandler (1 of 2) void setDocumentLocator(Locator locator); void startDocument(); void endDocument(); void processingInstruction(String target, String data); void skippedEntity(String name);

org.xml.sax.ContentHandler (2 of 2) void startPrefixMapping(String prefix, String uri); void endPrefixMapping(String prefix); void startElement(String uri, String localName, String qname, Attributes attrs); void endElement(String uri, String localName, String qname); void characters(char[ ] ch, int offset, int length); void ignorableWhitespace(char[ ] ch, int offset, int length);

Receiving SAX Events (1 of 4) Implementing an element counter 01public class ElementCounter extends DefaultHandler { 02 private int elements; 03 public void startDocument() { 04 elements = 0; 05 } 06 public void startElement(String uri, String localName, 07 String qname, Attributes attributes) { 08 elements++; 09 } 10 public void endDocument() { 11 System.out.println(“elements: “+elements); 12 } 13}

Receiving SAX Events (2 of 4) Implementing an element counter 01public class ElementCounter extends DefaultHandler { 02 private int elements; 03 public void startDocument() { 04 elements = 0; 05 } 06 public void startElement(String uri, String localName, 07 String qname, Attributes attributes) { 08 elements++; 09 } 10 public void endDocument() { 11 System.out.println(“elements: “+elements); 12 } 13}

Receiving SAX Events (3 of 4) Implementing an element counter 01public class ElementCounter extends DefaultHandler { 02 private int elements; 03 public void startDocument() { 04 elements = 0; 05 } 06 public void startElement(String uri, String localName, 07 String qname, Attributes attributes) { 08 elements++; 09 } 10 public void endDocument() { 11 System.out.println(“elements: “+elements); 12 } 13}

Receiving SAX Events (4 of 4) Registering custom content handler Input – Andy Clark Output – elements: 3 01XMLReader reader = XMLReaderFactory.createXMLReader(); 02reader.setFeature(“ true); 03reader.setFeature(“ false); 04reader.setContentHandler(new ElementCounter()); 05reader.parse(“document.xml”);

Receiving Errors (1 of 4) Implementing error handler 01public class ErrorPrinter implements ErrorHandler { 02 public void warning(SAXParseException e) { 03 System.out.println(“[warning] “+e.getMessage()); 04 } 05 public void error(SAXParseException e) { 06 System.out.println(“[error] “+e.getMessage()); 07 } 08 public void fatalError(SAXParseException e) throws SAXException { 09 System.out.println(“[fatal error] “+e.getMessage()); 10 throw e; 11 } 12}

Receiving Errors (2 of 4) Implementing error handler 01public class ErrorPrinter implements ErrorHandler { 02 public void warning(SAXParseException e) { 03 System.out.println(“[warning] “+e.getMessage()); 04 } 05 public void error(SAXParseException e) { 06 System.out.println(“[error] “+e.getMessage()); 07 } 08 public void fatalError(SAXParseException e) throws SAXException { 09 System.out.println(“[fatal error] “+e.getMessage()); 10 throw e; 11 } 12}

Receiving Errors (3 of 4) Implementing error handler 01public class ErrorPrinter implements ErrorHandler { 02 public void warning(SAXParseException e) { 03 System.out.println(“[warning] “+e.getMessage()); 04 } 05 public void error(SAXParseException e) { 06 System.out.println(“[error] “+e.getMessage()); 07 } 08 public void fatalError(SAXParseException e) throws SAXException { 09 System.out.println(“[fatal error] “+e.getMessage()); 10 throw e; 11 } 12}

Receiving Errors (4 of 4) Registering custom error handler Input – Andy Clark Output – [error] Document root element "name", must match DOCTYPE root "null". – [error] Document is invalid: no grammar found. 01XMLReader reader = XMLReaderFactory.createXMLReader(); 02reader.setFeature(“ true); 03reader.setFeature(“ true); 04reader.setErrorHandler(new ErrorPrinter()); 05reader.parse(“document.xml”);

Resolving Entities (1 of 4) Implementing entity resolver 01public class ResolverPrinter implements EntityResolver { 02 public InputSource resolveEntity(String publicId, String systemId) { 03 System.out.println(“publicId: “+publicId); 04 System.out.println(“systemId: “+systemId); 05 return null; 06 } 07}

Resolving Entities (2 of 4) Implementing entity resolver 01public class ResolverPrinter implements EntityResolver { 02 public InputSource resolveEntity(String publicId, String systemId) { 03 System.out.println(“publicId: “+publicId); 04 System.out.println(“systemId: “+systemId); 05 return null; 06 } 07}

Resolving Entities (3 of 4) Implementing entity resolver – Note: Always set the system identifier on the input source returned from your custom entity resolver. 01public class ResolverPrinter implements EntityResolver { 02 public InputSource resolveEntity(String publicId, String systemId) { 03 System.out.println(“publicId: “+publicId); 04 System.out.println(“systemId: “+systemId); 05 return null; 06 } 07}

Resolving Entities (4 of 4) Registering custom entity resolver Input – Output – publicId: null – systemId: file:///D:/xml/examples/grammar.dtd 01XMLReader reader = XMLReaderFactory.createXMLReader(); 02reader.setFeature(“ true); 03reader.setFeature(“ false); 04reader.setEntityResolver(new ResolverPrinter()); 05reader.parse(“document.xml”);

Useful Links SAX –

XML Programming: SAX Andy Clark