Presentation is loading. Please wait.

Presentation is loading. Please wait.

Beginning XML 4th Edition. Chapter 12: Simple API for XML (SAX)

Similar presentations


Presentation on theme: "Beginning XML 4th Edition. Chapter 12: Simple API for XML (SAX)"— Presentation transcript:

1 Beginning XML 4th Edition

2 Chapter 12: Simple API for XML (SAX)

3 Chapter 12 Objectives What is SAX? Where to download SAX and how to set it up How and when to use the primary SAX interfaces

4 What is SAX? Parser analyzes XML documents can be more efficient than DOM

5 A Simple Analogy Like watching a train go by Note the Start of the train End of the train Start of each car End of each car Color Length Engineer Baggage Passengers

6 A Brief History of SAX Not a formal specification It is popular because it works! Since 1997 Courtesy of David Megginson

7 Where to Get SAX Xerces2-J AEIfred2 Crimson Oracle XP

8 Setting Up SAX SAX libraries Parser JDK

9 Receiving SAX Events public class MyClass implements ContentHandler public class MyClass extends DefaultHandler

10 ContentHandler Interface startDocumentprocessingInstruction endDocumentstartPrefixMapping startElementendPrefixMapping characterssetDocumentLocator ignorableWhitespaceskippedEntity

11 Try It Out The Start of Something Big

12 Handling Element Events public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException ParameterValue urihttp://example.com localNamemyElement qNamemyPrefix:myElement

13 Attributes MethodDescription getLengthDetermine the number of attributes available in the Attributes interface. getIndexRetrieve the index of a specific attribute in the list. The getIndex function allows you to look up the index by using the attribute’s qualified name or by using both the local name and namespace URI. getLocalNameRetrieve a specific attribute’s local name by sending the index in the list. getQNameRetrieve a specific attribute’s qualified name by sending the index in the list. getURIRetrieve a specific attribute’s namespace URI by sending the index in the list. getTypeRetrieve a specific attribute’s type by sending the index in the list, by using the attribute’s qualified name, or by using both the local name and namespace URI. If there is no Document Type Definition (DTD), this function will always return CDATA. getValueRetrieve a specific attribute’s value by sending the index in the list, by using the attribute’s qualified name, or by using both the local name and namespace URI.

14 Try It Out Element and Attribute Events

15 Handling Character Content public void characters(char[] ch, int start, int len) throws SAXException

16 Try It Out Adding Colorful Characters

17 When to Ignore ignorableWhitespace public void ignorableWhitespace(char[ ] ch, int start, int len) throws SAXException

18 Skipped Entities public void skippedEntity(String name) throws SAXException

19 Handling Special Commands with Processing Instructions public void processingInstruction(String target, String data) throws SAXException

20 Namespace Prefixes public void startPrefixMapping(String prefix, String uri) throws SAXException public void endPrefixMapping(String prefix) throws SAXException xmlns:example=“http://example.com”

21 Try It Out Pulling the Brakes

22 Providing the Location of the Error MethodDescription getLineNumberRetrieves the line number for the current event. getColumnNumberRetrieves the column number for the current event (the SAX specification assumes that the column number is based on right-to-left reading modes). getSystemIdRetrieves the system identifier of the document for the current event. Because XML documents may be composed of multiple external entities, this may change throughout the parsing process. getPublicIdRetrieves the public identifier of the document for the current event. Because XML documents may be composed of multiple external entities, this may change throughout the parsing process.

23 Try It Out Which Stop is This?

24 ErrorHandler Interface EventDescription warningAllows the parser to notify the application of a warning it has encountered in the parsing process. Though the XML Recommendation provides many possible warning conditions, very few SAX parsers actually produce warnings. errorAllows the parser to notify the application that it has encountered an error. Even though the parser has encountered an error, parsing can continue. Validation errors should be reported through this event. fatalErrorAllows the parser to notify the application that it has encountered a fatal error and cannot continue parsing. Well- formedness errors should be reported through this event.

25 Try It Out To Err Is Human, To Handle Errors Is Divine

26 DTDHandler Interface EventDescription notationDeclAllows the parser to notify the application that it has read a notation declaration. unparsedEntityDeclAllows the parser to notify the application that it has read an unparsed entity declaration.

27 EntityResolver Interface EventDescription resolveEntityAllows the application to handle the resolution of entity lookups for the parser. reader.setEntityResolver(this); <!ENTITY train PUBLIC "-//TRAINS//freight cars xml 1.0//EN" "http://example.com/freighttrain.xml">

28 Features and Properties public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException

29 Working with Properties public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException

30 Extension Interfaces: DeclHandler EventDescription attributeDeclAllows the parser to notify the application that it has read an attribute declaration elementDeclAllows the parser to notify the application that it has read an element declaration. externalEntityDeclAllows the parser to notify the application that it has read an external entity declaration. internalEntityDeclAllows the parser to notify the application that it has read an internal entity declaration.

31 Extention Interfaces: LexicalHandler EventDescription commentAllows the parser to notify the document that it has read a comment. The entire comment will be passed back to the application in one event call; it will not be buffered as it may be in the characters and ignorableWhitespace events. startCDATAAllows the parser to notify the document that it has encountered a CDATA section start marker. The character data within the CDATA section will always be passed to the application through the characters event. endCDATAAllows the parser to notify the document that it has encountered a CDATA section end marker. startDTDAllows the parser to notify the document that it has begun reading a DTD. endDTDAllows the parser to notify the document that it has finished reading a DTD. startEntityAllows the parser to notify the document that it has started reading or expanding an entity. endEntityAllows the parser to notify the document that it has finished reading or expanding an entity.

32 Good SAX and Bad SAX Good Simple Doesn’t load whole document Small footprint Quick Filtering Bad No control over order Intricate state keeping required DOM is better at analyzing

33 Consumers, Producers, and Filters Sometimes it’s easier to filter than to write an application to consume and produce events.

34 Other Languages LanguageAvailable Interfaces C++Xerces-C++, the counterpart to the Xerces-J toolkit you are using from Apache, defines a set of C and C++ bindings available at http://xml.apache.org/xerces-c. Microsoft Core XML Services (formerly MSXML), provides C++ and COM interfaces (including ActiveX wrappers) available at http://msdn.microsoft.com/xml. Arabica toolkit provides C++ bindings that make more extensive use of C++ language features, available at http://www.jezuk.co.uk/cgi-bin/view/Arabica. Perl SAX bindings for Perl can be found at http://perl-xml.sourceforge.net/libxml-perl/. PythonPython 2.0 includes support for SAX processing in its markup toolkit as part of the default distribution available at http://www.python.org/. PascalSAX for Pascal bindings can be found at http://saxforpascal.sourceforge.net/. Visual BasicMSXML, the Microsoft XML toolkit, provides Visual Basic interfaces available at http://msdn.microsoft.com/library/en-us/xmlsdk/html/ac6be45a-177e-4b80-a918- dc73e357f7bb.asp..NETThe System.Xml classes distributed with.NET provide psuedo-SAX implementations usable in various.NET languages. The interfaces are "pull-based" and are not exact correlations. To use SAX interfaces in.NET visit the SAX for.NET project at http://saxdotnet.sourceforge.net. CurlCurl is a web content management system with its own SAX bindings, available at http://www.curl.com/.


Download ppt "Beginning XML 4th Edition. Chapter 12: Simple API for XML (SAX)"

Similar presentations


Ads by Google