DOC PREVIEW
Penn CIT 597 - SAX LECTURE NOTES

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

SAXSAX, DOM, and XOMDifference between SAX and DOMCallbacksSimple SAX programThe Sample class, IThe Sample class, IIThe Handler class, IThe Handler class, IIResultsMore resultsFactoriesParser factoriesGetting a parserDeclaring which handler to useSAX handlersClass DefaultHandlerContentHandler methods, IContentHandler methods, IIContentHandler methods, IIIContentHandler methods, IVAttributes, IAttributes, IIContentHandler methods, VContentHandler methods, VIExampleWhitespaceHandling ignorable whitespaceError Handling, IError Handling, IIError Handling, IIIThe EndJan 13, 2019SAX2SAX, DOM, and XOM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 (but very popular) standardSAX was developed by David Megginson and is open sourceThere are various implementations availableJava implementations are provided as part of JAXP (Java API for XML Processing)JAXP is included as a package in Java 1.4 and Java 5JAXP is available separately for Java 1.3XOM is a new parser by Elliott Rusty HaroldUnlike many XML technologies, XML parsers are relatively easy3Difference between SAX and DOMDOM reads the entire XML document into memory and stores it as a tree data structureSAX reads the XML document and calls one of your methods for each element or block of text that it encountersConsequences:DOM provides “random access” into the XML documentSAX provides only sequential access to the XML documentDOM is slow and requires huge amounts of memory, so it cannot be used for large XML documentsSAX is fast and requires very little memory, so it can be used for huge documents (or large numbers of documents)This makes SAX much more popular for web sitesSome DOM implementations have methods for changing the XML document in memory; SAX implementations do not4CallbacksSAX works through callbacks: you call the parser, it calls methods that you supplyYour programmain(...)startDocument(...) startElement(...)characters(...)endElement( )endDocument( )parse(...)The SAX parser5Simple SAX programThe following program is adapted from CodeNotes® for XML by Gregory Brill, pages 158-159The program consists of two classes:Sample -- This class contains the main method; itGets a factory to make parsersGets a parser from the factoryCreates a Handler object to handle callbacks from the parserTells the parser which handler to send its callbacks toReads and parses the input XML fileHandler -- This class contains handlers for three kinds of callbacks:startElement callbacks, generated when a start tag is seenendElement callbacks, generated when an end tag is seencharacters callbacks, generated for the contents of an element6The Sample class, Iimport javax.xml.parsers.*; // for both SAX and DOMimport org.xml.sax.*;import org.xml.sax.helpers.*;// For simplicity, we let the operating system handle exceptions// In "real life" this is poor programming practicepublic class Sample { public static void main(String args[]) throws Exception { // Create a parser factory SAXParserFactory factory = SAXParserFactory.newInstance(); // Tell factory that the parser must understand namespaces factory.setNamespaceAware(true); // Make the parser SAXParser saxParser = factory.newSAXParser(); XMLReader parser = saxParser.getXMLReader();7The Sample class, IIIn the previous slide we made a parser, of type XMLReader // Create a handler Handler handler = new Handler(); // Tell the parser to use this handler parser.setContentHandler(handler); // Finally, read and parse the document parser.parse("hello.xml"); } // end of Sample classYou will need to put the file hello.xml :In the same directory, if you run the program from the command lineOr where it can be found by the particular IDE you are using8The Handler class, Ipublic class Handler extends DefaultHandler {DefaultHandler is an adapter class that defines these methods and others as do-nothing methods, to be overridden as desiredWe will define three very similar methods to handle (1) start tags, (2) contents, and (3) end tags--our methods will just print a lineEach of these three methods could throw a SAXException // SAX calls this method when it encounters a start tag public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes attributes) throws SAXException { System.out.println("startElement: " + qualifiedName); }9The Handler class, II // SAX calls this method to pass in character data public void characters(char ch[ ], int start, int length) throws SAXException { System.out.println("characters: \"" + new String(ch, start, length) + "\""); }  // SAX call this method when it encounters an end tag public void endElement(String namespaceURI, String localName, String qualifiedName) throws SAXException { System.out.println("Element: /" + qualifiedName); }} // End of Handler class10ResultsIf the file hello.xml contains: <?xml version="1.0"?> <display>Hello World!</display>Then the output from running java Sample will be: startElement: display characters: "Hello World!" Element: /display11More resultsNow suppose the file hello.xml contains:<?xml version="1.0"?><display> <i>Hello</i> World!</display>Notice that the root element, <display>, now contains a nested element <i> and some whitespace (including newlines)The result will be as shown at the right:startElement: displaycharacters: ""characters: "" characters: " " startElement: icharacters: "Hello"endElement: /icharacters: "World!"characters: " "endElement: /display// empty string// newline// spaces// another newline12FactoriesSAX uses a parser factoryA factory is an alternative to constructorsFactories allow the programmer to:Decide whether or not to create a new objectDecide what kind (subclass, implementation) of object to createTrivial example:class TrustMe { private TrustMe() { } // private constructor public TrustMe makeTrust() { // factory method if ( /* test of some sort */) return new TrustMe(); } }}13Parser factoriesTo create a SAX parser factory,


View Full Document

Penn CIT 597 - SAX LECTURE NOTES

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download SAX LECTURE NOTES
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 SAX LECTURE NOTES 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 SAX LECTURE NOTES 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?