1 / 62

Before today’s lecture

Before today’s lecture. Personal Project Due date (including demo your work): 4/12 Grading scheme. Before today’s lecture. Final Project Group members : Deadline (for grouping your members): Before 4/10 Send the name list of your group members to 尚純 or 紹楷

samara
Télécharger la présentation

Before today’s lecture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Before today’s lecture • Personal Project • Due date (including demo your work): 4/12 • Grading scheme

  2. Before today’s lecture • Final Project • Group members: • Deadline (for grouping your members): Before 4/10 • Send the name list of your group members to 尚純 or 紹楷 • For those who can’t make a team, we’ll make a group for you. The group members will be posted on 4/12 • If you want to make a change, the deadline is on 4/15 • Project Topics: • Will be posted on the web, pick one and send your topic to尚純 or 紹楷 • Alternatively, send a proposal for selecting your own topic. • The proposal should include reference information of the topic and the scope of the project. • Teaching Assisstants: 吳尚純 knaughtally@hotmail.com李紹楷 inses1982@yahoo.com.tw

  3. Simple API for XML (SAX)Is SAX too hard for mortal programmers? And is the domination of DOM a bad thing?

  4. Introduction • XML Parsing Operations • The SAX API • How SAX Processing Works • SAX-based parsers • Events • An SAX Example: Step by Step • Example (SAX1.0): Tree Diagram • SAX 2.0 • Example: Printing the notes in an XML document • Summary

  5. Character Stream Standardized XML APIs Serializer Parser Introduction • Processing XML • Create a Parser object Point the object to an XML doc.  Process • Basic Operations for processing an XML document • A basic XML processing architecture • 3 key layers: XML documents, The application, infrastructure for working with XML doc. XML Document(s) Application

  6. Introduction (cont.) • Basic Operations (cont.) • Parsing is the first step that enables an application to work with an XML doc. • Parsing process breaks up the text of an XML document into small identifiable pieces (nodes) • Parser will break documents into pieces, recognized as start-end tags, attribute value pairs, chunks of text content, processing instructions, comments, and so on. • These pieces are fed into application through well-defined APIs implementing a particular parsing model • Four parsing models are commonly in use:

  7. Introduction (cont.) • Basic Operations (cont.) • Four parsing models are commonly in use: • Pull Parsing • The application always ask the parser to give it the next piece of information • It is as if the app. has to “pull” the info. out of the parser, activate the communication by the app. • The XML community has not yet defined standard APIs for the “pull parsing” • It could happen soon because of its popularity! • Push Parsing • The parser sends notifications to the application during the parsing process • The notifications are sent in “reading” order (i.e., their appearance order in the document)

  8. Introduction (cont.) • Basic Operations (cont.) • Push Parsing • Notifications are typically implemented as event callbacks in the application • Known as event-based parsing • Simple API for XML (SAX) is the standard for push parsing • One-step Parsing • The parser reads the whole XML doc. and generates a data structure (a parse tree) describing its entire contents (elements, attributes,… etc.) • W3C Standard : XML DOM (Document Object Model): specifies the types of objects that will be included in the parse tree, their properties, and operations • The DOM is a language- and platform-independent API. • The biggest problem is memory overhead and computational efficiency

  9. Introduction (cont.) • Basic Operations (cont.) • Hybrid Parsing • Combine the characteristics of the other two parsing models to create efficient parsers for special scenarios • Lets break the concept of loading and parsing to analyse the condition • Loading the document: one-step parsing • Parsing the rest of the document: providing partial information extracted from the document for the application • For example, Push + one-step parsing • The application thinks it is working with a one-step parser; in reality, the parsing process has just begun • As the application keep accessing more objects on the DOM tree, the parsing continues incrementally • Just enough of the XML document is parsed at any given point to give the application the objects it wants to see

  10. An example of hybrid parsing • In Sun's reference implementation, the DOM API builds on the SAX API as shown in the diagram, • Sun's implementation of the Document Object Model (DOM) API uses the SAX libraries to read in XML data and construct the tree of data objects that constitutes the DOM. • Sun's implementation also provides a framework to help output the object tree as XML data

  11. Introduction (cont.) • Why define many models? • Trade-offs between memory efficiency, computational efficiency, and ease of programming • A table is presented to compare the trade-offs of the models

  12. Introduction (cont.) • How to choose between SAX and DOM: Whether you choose DOM or SAX is going to depend on several factors: • Purpose of the application: • To make changes to the data and output it as XML, then in most cases, DOM is the way to go. • SAX is much more complex to program, as you'd have to make changes to a copy of the data rather than to the data itself. • Amount of data: For large files, SAX is a better bet. • How the data will be used: If only a small amount of the data will actually be used, you may be better off using SAX to extract it into your application. • On the other hand, if you know that you will need to refer back to large amounts of information that has already been processed, SAX is probably not the right choice. • The need for speed: SAX implementations are normally faster than DOM implementations. • It's important to remember that SAX and DOM are not mutually exclusive. • Use DOM to createa stream of SAX events, • Use SAX to createa DOM tree. • In fact, most parsers used to create DOM trees are actually using SAX to do it!

  13. The SAX APIs • SAX (The Simple API for XML ) • SAX is the Simple API for XML, originally a Java-only API. • SAX was the first widely adopted API for XML in Java, and is a “de facto” standard. • The current version is SAX 2.0.x, and there are versions for several programming language environments other than Java • Another method for accessing XML document’s contents • Developed by XML-DEV mailing-list members • Uses event-based model • Notifications (events) are raised as document is parsed

  14. The SAX APIs (cont.) • SAX Parsing architecture: using the common abstract factory design pattern • Create an instance of SAXParserFactory (used to create an instance of SAX Parser) • SAXReader: event trigger, when the parse() method is invoked, the reader starts firing events to the application by invoking registered callbacks • Those methods are defined by the interfaces ContentHandler, ErrorHandler, DTDHandler, and EntityResolver.

  15. The SAX APIs (cont.) • Here is a summary of the key objects in SAX APIs: • SAXParserFactory Creates an instance of the parser determined by the system property, javax.xml.parsers.SAXParserFactory • SAXParser Defines several kinds of parse() methods. In general, you pass an XML data source and a DefaultHandler object to the parser, which processes the XML and invokes the appropriate methods in the handler object. • SAXReader Carries on the conversation with the SAX event handlers you define

  16. The SAX APIs (cont.) • DefaultHandler Implements the ContentHandler, ErrorHandler, DTDHandler, and EntityResolverinterfaces (with null methods), so you can override only the ones you're interested in. • ContentHandler Defines methods, which are invoked when the parser encounters the text in an XML element or an inline processing instruction, respectively. • ErrorHandler Methods in response to various parsing errors. • DTDHandler Defines methods you will generally never be called upon to use. Used when processing a DTD to recognize and act on declarations for an unparsed entity.

  17. The SAX APIs (cont.) • Being event-based means that the parser reads an XML document from beginning to end, • Each time it recognizes a syntax construction, it notifies the application that is running it • The SAX parser notifies the application by calling methods from the ContentHandler interface. • For example, when the parser comes to a less than symbol ("<"), it calls the startElement method;

  18. The SAX API (cont.) • when it comes to character data, it calls the characters method; • when it comes to the less than symbol followed by a slash ("</"), it calls the endElement method • To illustrate, let's look at an example XML document and walk through what the parser does for each line.

  19. How SAX Processing Works • <?xml version="1.0"?> • <samples> • <server>UNIX</server> • <monitor>color</monitor> • </samples> • SAX analyzes an XML stream as it goes by, much like an old ticker tape. • Consider the following XML code snippet: Start document Start element (samples) Characters (white space) Start element (server) Characters (UNIX) End element (server) Characters (white space) Start element (monitor) Characters (color) End element (monitor) Characters (white space) End element (samples) • A SAX processor analyzing this code snippet would generate, in general, the following events:

  20. How SAX Processing Works (cont.) • The SAX API allows a developer to capture these events and act on them • What does “the developer” represent for? • SAX processing involves the following steps: • Create an event handler. • Create the SAX parser. • Assign the event handler to the parser. • Parse the document, sending each event to the handler.

  21. How SAX Processing Works (cont.) • The pros and cons of event-based processing • The advantages of this kind of processing are much like the advantages of streaming media. (like interpreter?) • Analysis can get started immediately, rather than waiting for all of the data to be processed. • The application is simply examining the data as it goes by, it doesn't need to store it in memory: • A huge advantage when it comes to large documents.

  22. How SAX Processing Works (cont.) • The pros and cons of event-based processing • In fact, an application doesn't even have to parse the entire document; • Stop when certain criteria have been satisfied. • In general, SAX is also much faster than the alternative, the DOM. • On the other hand, because the application is not storing the data in any way, • it is impossible to make changes to it using SAX, or to move backwards in the data stream.

  23. SAX-based Parsers • SAX-based parsers • Use Sun Microsystem’s JAXP in Textbook • Tools • A text editor: XML files are simply text. To create and read them, a text editor is all you need. • JavaTM 2 SDK, Standard Edition version 1.4.x: SAX support has been built into the latest version of Java (available at http://java.sun.com/j2se/1.4.2/download.html), won't need to install any separate classes. Using an earlier version of Java, such as Java 1.3.x, you'll also need • an XML parser such as the Apache project's Xerces-Java (available at http://xml.apache.org/xerces2-j/index.html), • or Sun's Java API for XML Parsing (JAXP), part of the Java Web Services Developer Pack (available at http://java.sun.com/webservices/downloads/webservicespack.html). • You can also download the official version from SourceForge (available at http://sourceforge.net/project/showfiles.php?group_id=29449). • Other Languages: Should you wish to adapt the examples, SAX implementations are also available in other programming languages. • You can find information on C, C++, Visual Basic, Perl, and Python implementations of a SAX parser at http://www.saxproject.org/?selected=langs.

  24. Some SAX-based parsers.

  25. Setup • Java applications to illustrate SAX API • Java 2 Standard Edition required • Download at www.java.sun.com/j2se • Installation instructions • www.deitel.com/faq/java3install.htm • JAXP required • Download at java.sun.com/xml/download.html

  26. Events • SAX parser • Invokes certain methods (Fig. 9.2) when events occur • Programmers override these methods to process data

  27. Fig. 9.2 Methods invoked by the SAX parser

  28. The SAX API – an Example <priceList>[parser calls startElement] <coffee>  [parser calls startElement] <name>Mocha Java</name> [parser calls startElement, characters, and endElement] <price>11.95</price> [parser calls startElement, characters, and endElement] </coffee>  [parser calls endElement] <priceList>[parser calls endElement] • The default implementations of the methods that the parser calls do nothing • You need to write a subclass implementing the appropriate methods to get the functionality you want • For example, suppose you want to get the price per pound for Mocha Java. • You would write a class extending DefaultHandler (the default implementation of ContentHandler) in which you write your own implementations of the methods startElement and characters

  29. The SAX API – an Example (cont.) • You code has three tasks. • Scan the command line for the name (or URI) of an XML file. • Create a parser object. • Tell the parser object to parse the XML file named on the command line, and tell it to send your code all of the SAX events it generates. • Step I: Scan the command line • For an argument. If there isn't an argument, you print an error message and exit. • Otherwise, assume that the first argument is the name or URI of an XML file • public static void main(String argv[]) { • if (argv.length == 0 || (argv.length == 1 && argv[0].equals("-help"))) { • // Print an error message and exit... • } • PrintOutline s1 = new PrintOutline(); • s1.parseURI(argv[0]); • }

  30. The SAX API – an Example (cont.) • Step II: Create a parser object • To create a parser object, use JAXP's SAXParserFactory API to create a SAXParser • public void parseURI(String uri) { • try { • SAXParserFactory spf = SAXParserFactory.newInstance(); • SAXParser sp = spf.newSAXParser(); • . . .

  31. The SAX API – an Example (cont.) • Step 3: Parse the file and handle any events • We've created our parser object, we need to have it parse the file. That's done with the parse() method • Notice that the parse() method takes two arguments. The first is the URI of the XML document, while the second is an object that implements the SAX event handlers • public void parseURI(String uri) { • try { • SAXParserFactory spf = SAXParserFactory.newInstance(); • SAXParser sp = spf.newSAXParser(); • sp.parse(uri, this); • } catch (Exception e) { • System.err.println(e); • } • }

  32. The SAX API – an Example (cont.) • In the case of PrintOutline, you're extending the SAX DefaultHandler interface: • DefaultHandler has an implementation of a number of event handlers. These implementations do nothing, which means all your code has to do is implement handlers for the events you care about. • Note: The exception handling above is sloppy; as an exercise for the reader, feel free to handle specific exceptions, such as SAXException or java.io.IOException. • A major benefit of the DefaultHandler interface is that it shields you from having to implement all of the event handlers. • DefaultHandler implements all of the event handlers; you just implement the ones you care about. public class PrintOutlineextends DefaultHandler{ ……. }

  33. The SAX API – an Example (cont.) • Step IV: Implementing event handlers • startdocument() event handlers • Simply writing out a basic XML declaration, regardless of whether one was in the original XML document or not. • Currently the base SAX API doesn't return the details of the XML declaration public void startDocument() { System.out.println("<?xml version=\"1.0\"?>"); }

  34. The SAX API – an Example (cont.) • Next, here's what you do for startElement(): • Print the name of the elements and attributes • Namespace URI in braces before the element's local name • rawName contains the raw XML 1.0 name if a namespace URI doesn't have public void startElement(String namespaceURI, String localName, String rawName, Attributes attrs) { System.out.print("<"); System.out.print(rawName); if (attrs != null) { int len = attrs.getLength(); for (int i = 0; i < len; i++) { System.out.print(" "); System.out.print(attrs.getQName(i)); System.out.print("=\""); System.out.print(attrs.getValue(i)); System.out.print("\""); } } System.out.print(">"); }

  35. The SAX API – an Example (cont.) • More event handling • characters() : printing the XML document to the console, you're simply printing the portion of the character array that relates to this event public void characters(char ch[ ], int start, int length) { System.out.print(new String(ch, start, length)); } • endElement() : simply write out the end tag • endDocument() : Do nothing just for the completeness. public void endElement(String namespaceURI, String localName, String rawName) { System.out.print("</"); System.out.print(rawName); System.out.print(">"); } public void endDocument() { System.out.println("End of Document"); }

  36. The SAX API – an Example (cont.) • Step V: Error handling: • SAX defines the ErrorHandler interface; • Implemented by DefaultHandler; • contains three methods: warning, error, and fatalError (defined by the XML specification ) • warning(): Issued in response to a warning • error(): Issued in response to an error condition. • fatalError(): Issued in response to a fatal error public void warning(SAXParseException ex) { System.err.println("[Warning] "+ getLocationString(ex)+": "+ ex.getMessage()); } public void error(SAXParseException ex) { System.err.println("[Error] "+ getLocationString(ex)+": "+ ex.getMessage()); } public void fatalError(SAXParseException ex) throws SAXException { System.err.println("[Fatal Error] "+ getLocationString(ex)+": "+ ex.getMessage()); throw ex; }

  37. Example: Tree Diagram • Java application • Parse XML document with SAX-based parser • Output document data as tree diagram • extendsorg.xml.sax.HandlerBase • implements interface EntityResolver • Handles external entities • implements interface DTDHandler • Handles notations and unparsed entities • implements interface DocumentHandler • Handles parsing events • implements interface ErrorHandler • Handles errors

  38. 1 // Fig. 9.3 : Tree.java 2 // Using the SAX Parser to generate a tree diagram. 3 import specifies location of classes needed by application 4 import java.io.*; 5 import org.xml.sax.*; // for HandlerBase class 6 import javax.xml.parsers.SAXParserFactory; Assists in formatting 7 import javax.xml.parsers.ParserConfigurationException; 8 import javax.xml.parsers.SAXParser; 9 Override method to output parsed document’s URL 10 public class Tree extends HandlerBase { 11 private int indent = 0; // indentation counter 12 13 // returns the spaces needed for indenting 14 private String spacer( int count ) 15 { 16 String temp = ""; 17 18 for ( int i = 0; i < count; i++ ) 19 temp += " "; 20 21 return temp; 22 } 23 24 // method called before parsing 25 // it provides the document location 26 public void setDocumentLocator( Locator loc ) 27 { 28 System.out.println( "URL: " + loc.getSystemId() ); 29 } 30 Fig. 9.3 Application to create a tree diagram for an XML document.import specifies location of classes needed by applicationAssists in formattingOverride method to output parsed document’s URL

  39. 31 // method called at the beginning of a document Overridden method called when root node encountered 32 public void startDocument() throws SAXException 33 { Overridden method called when end of document is encountered 34 System.out.println( "[ document root ]" ); 35 } 36 Overridden method called when start tag is encountered 37 // method called at the end of the document 38 public void endDocument() throws SAXException Output each attribute’s name and value (if any) 39 { 40 System.out.println( "[ document end ]" ); 41 } 42 43 // method called at the start tag of an element 44 public void startElement( String name, 45 AttributeList attributes ) throws SAXException 46 { 47 System.out.println( spacer( indent++ ) + 48 "+-[ element : " + name + " ]"); 49 50 if ( attributes != null ) 51 52 for ( int i = 0; i < attributes.getLength(); i++ ) 53 System.out.println( spacer( indent ) + 54 "+-[ attribute : " + attributes.getName( i ) + 55 " ] \"" + attributes.getValue( i ) + "\"" ); 56 } 57 Fig. 9.3 Application to create a tree diagram for an XML document. (Part 2)Overridden method called when root node encounteredOverridden method called when end of document is encounteredOverridden method called when start tag is encounteredOutput each attribute’s name and value (if any)

  40. 58 // method called at the end tag of an element 59 public void endElement( String name ) throws SAXException Overridden method called when end of element is encountered 60 { 61 indent--; Overridden method called when processing instruction is encountered 62 } 63 64 // method called when a processing instruction is found Overridden method called when character data is encountered 65 public void processingInstruction( String target, 66 String value ) throws SAXException 67 { 68 System.out.println( spacer( indent ) + 69 "+-[ proc-inst : " + target + " ] \"" + value + "\"" ); 70 } 71 72 // method called when characters are found 73 public void characters( char buffer[], int offset, 74 int length ) throws SAXException 75 { 76 if ( length > 0 ) { 77 String temp = new String( buffer, offset, length ); 78 79 System.out.println( spacer( indent ) + 80 "+-[ text ] \"" + temp + "\"" ); 81 } 82 } 83 Fig. 9.3 Application to create a tree diagram for an XML document. (Part 3)Overridden method called when end of element is encounteredOverridden method called when processing instruction is encounteredOverridden method called when character data is encountered

  41. 84 // method called when ignorable whitespace is found 85 public void ignorableWhitespace( char buffer[], 86 int offset, int length ) 87 { 88 if ( length > 0 ) { 89 System.out.println( spacer( indent ) + "+-[ ignorable ]" ); 90 } Overridden method called when ignorable whitespace is encountered Overridden method called when error (usually validation) occurs Overridden method called when problem is detected (but not considered error) 91 } 92 93 // method called on a non-fatal (validation) error Method main starts application 94 public void error( SAXParseException spe ) 95 throws SAXParseException 96 { 97 // treat non-fatal errors as fatal errors 98 throw spe; 99 } 100 101 // method called on a parsing warning 102 public void warning( SAXParseException spe ) 103 throws SAXParseException 104 { 105 System.err.println( "Warning: " + spe.getMessage() ); 106 } 107 108 // main method 109 public static voidmain( String args[] ) 110 { 111 boolean validate = false; 112 Fig. 9.3 Application to create a tree diagram for an XML document. (Part 4)Overridden method called when ignorable whitespace is encounteredOverridden method called when error (usually validation) occursOverridden method called when problem is detected (but not considered error)Method main starts application

  42. 113 if ( args.length != 2 ) { 114 System.err.println( "Usage: java Tree [validate] " + 115 "[filename]\n" ); 116 System.err.println( "Options:" ); Allow command-line arguments (if we want to validate document) 117 System.err.println( " validate [yes|no] : " + 118 "DTD validation" ); 119 System.exit( 1 ); 120 } SAXParserFactory can instantiate SAX-based parser 121 122 if ( args[ 0 ].equals( "yes" ) ) 123 validate = true; 124 125 SAXParserFactory saxFactory = 126 SAXParserFactory.newInstance(); 127 128 saxFactory.setValidating( validate ); 129 Fig. 9.3 Application to create a tree diagram for an XML document. (Part 5)Allow command-line arguments (if we want to validate document)SAXParserFactory can instantiate SAX-based parser

  43. 130 try { 131 SAXParser saxParser = saxFactory.newSAXParser(); 132 saxParser.parse( new File( args[ 1 ] ), new Tree() ); Instantiate SAX-based parser 133 } 134 catch ( SAXParseException spe ) { 135 System.err.println( "Parse Error: " + spe.getMessage() ); Handles errors (if any) 136 } 137 catch ( SAXException se ) { 138 se.printStackTrace(); 139 } 140 catch ( ParserConfigurationException pce ) { 141 pce.printStackTrace(); 142 } 143 catch ( IOException ioe ) { 144 ioe.printStackTrace(); 145 } 146 147 System.exit( 0 ); 148 } 149 } Fig. 9.3 Application to create a tree diagram for an XML document. (Part 6)Instantiate SAX-based parserHandles errors (if any)

  44. 1 <?xml version = "1.0"?> XML document does not reference DTD 2 XML document with elements test, example and object 3 <!-- Fig. 9.4 : spacing1.xml --> 4 <!-- Whitespaces in nonvalidating parsing --> Root element test contains attribute name with value “ spacing 1 ” 5 <!-- XML document without DTD --> 6 7 <test name = " spacing 1 "> 8 <example><object>World</object></example> Note that whitespace is preserved: attribute value (line 7), line feed (end of line 7), indentation (line 8) and line feed (end of line 8) 9 </test> Fig. 9.4 XML document spacing1.xml. XML document does not reference DTDXML document with elements test, example and objectRoot element test contains attribute name with value “ spacing 1 ”Note that whitespace is preserved: attribute value (line 7), line feed (end of line 7), indentation (line 8) and line feed (end of line 8) URL: file:C:/Tree/spacing1.xml[ document root ]+-[ element : test ] +-[ attribute : name ] " spacing 1 " +-[ text ] "" +-[ text ] " " +-[ element : example ] +-[ element : object ] +-[ text ] "World" +-[ text ] ""[ document end ]

  45. 1 <?xml version = "1.0"?> 2 DTD checks document’s characters, so any “removable” whitespace is ignorable 3 <!-- Fig. 9.5 : spacing2.xml --> 4 <!-- Whitespace and nonvalidated parsing --> 5 <!-- XML document with DTD --> 6 7 <!DOCTYPE test [ 8 <!ELEMENT test (example)> Line feed at line 14, spaces at beginning of line 15 and line feed at line 15 are ignored 9 <!ATTLIST test name CDATA #IMPLIED> 10 <!ELEMENT element (object*)> 11 <!ELEMENT object(#PCDATA)> 12 ]> 13 14 <test name = " spacing 2 "> 15 <example><object>World</object></example> 16 </test> Fig. 9.5 XML document spacing2.xml.DTD checks document’s characters, so any “removable” whitespace is ignorableLine feed at line 14, spaces at beginning of line 15 and line feed at line 15 are ignored URL: file:C:/Tree/spacing2.xml[ document root ]+-[ element : test ] +-[ attribute : name ] " spacing 2 " +-[ ignorable ] +-[ ignorable ] +-[ element : example ] +-[ element : object ] +-[ text ] "World" +-[ ignorable ][ document end ]

  46. 1 <?xml version = "1.0"?> Invalid document because element example cannot contain element item 2 3 <!-- Fig. 9.6 : notvalid.xml --> 4 <!-- Validation and non-validation --> 5 6 <!DOCTYPE test [ Validation disabled, so document parses successfully 7 <!ELEMENT test (example)> 8 <!ELEMENT example (#PCDATA)> Parser does not process text in CDATA section and returns character data 9 ]> 10 11 <test> 12 <?test message?> 13 <example><item><![CDATA[Hello & Welcome!]]></item></example> 14 </test> Fig. 9.6 Well-formed XML document.Invalid document because element example cannot contain element itemValidation disabled, so document parses successfullyParser does not process text in CDATA section and returns character data URL: file:C:/Tree/notvalid.xml[ document root ]+-[ element : test ] +-[ ignorable ] +-[ ignorable ] +-[ proc-inst : test ] "message" +-[ ignorable ] +-[ ignorable ] +-[ element : example ] +-[ element : item ] +-[ text ] "Hello & Welcome!" +-[ ignorable ][ document end ]

  47. Validation enabled Parsing terminates when fatal error occurs at element item URL: file:C:/Tree/notvalid.xml[ document root ]+-[ element : test ] +-[ ignorable ] +-[ ignorable ] +-[ proc-inst : test ] "message" +-[ ignorable ] +-[ ignorable ] +-[ element : example ]Parse Error: Element "example" does not allow "item" Fig. 9.6 Well-formed XML document.(Part 2)Validation enabledParsing terminates when fatal error occurs at element item

  48. 1 <?xml version = "1.0"?> 2 3 <!-- Fig. 9.7 : valid.xml --> 4 <!-- DTD-less document --> 5 6 <test> Validation disabled in first output, so document parses successfully Validation enabled in second output, and parsing fails because DTD does not exist 7 <example>Hello &amp; Welcome!</example> 8 </test> Fig. 9.7 Checking an XML document without a DTD for validity.Validation disabled in first output, so document parses successfullyValidation enabled in second output, and parsing fails because DTD does not exist URL: file:C:/Tree/valid.xml[ document root ]+-[ element : test ] +-[ text ] "" +-[ text ] " " +-[ element : example ] +-[ text ] "Hello " +-[ text ] "&" +-[ text ] " Welcome!" +-[ text ] ""[ document end ] URL: file:C:/Tree/valid.xml[ document root ]Warning: Valid documents must have a <!DOCTYPE declaration.Parse Error: Element type "test" is not declared.

  49. Example: Tree Diagram (Summary) • SAX 1.0 supported! • When compiling, the message, “Tree.java uses or overrides a deprecated API” “Recompile with –deprecation for details” • After compiling, 3 warning (class has been deprecated) were issued: • HandlerBase should be replaced by DefaultHandler • & 3. AttributeList should be replaced by Attributes • Better replace SAX1.0 with SAX2.0 • Problem with Xerces vs. JAXP

  50. SAX 2.0 • SAX 2.0 • Recently released • We have been using JAXP • Xerces parser (Apache) supports SAX 2.0

More Related