Index 1893115925 {398FAFD3} Java Collections [Zukowski 2001 04 26] 04. Oakley Natasha Królewski rod Ksiezniczka i magnat Sara Bell The way you say my name Gordon R. Dickson The Last Dream Dekorator Akunin Boris Berrigan, Selected Poems Zima lwów Jan Costin Wagner The Barker Triplets 2 Coll 132. Title Elise Mimo twoich wad Alan Dean Foster Damned 1 Call to Arms |
[ Pobierz całość w formacie PDF ] Throughout most of the DOM tutorial, you'll be using the sample slideshows you created in the SAX section. In particular, you'll use slideSample01.xml, a simple XML file with nothing much in it, and slideSample10.xml, a more complex example that includes a DTD, processing instructions, entity references, and a CDATA section. For instructions on how to compile and run your program, see Compiling the Program and Running the Program, from the SAX tutorial. Substitute "DomEcho" for "Echo" as the name of the program, and you're ready to roll. For now, just run the program on slideSample01.xml. If it ran without error, you have successfully parsed an XML document and constructed a DOM. Congratulations! Note: You'll have to take my word for it, for the moment, because at this point you don't have any way to display the results. But that is feature is coming shortly... Additional Information http://java.sun.com/xml/jaxp-1.1/docs/tutorial/dom/1_read.html (4 of 6) [8/22/2001 12:52:25 PM] 1. Reading Data into a DOM Now that you have successfully read in a DOM, there are one or two more things you need to know in order to use DocumentBuilder effectively. Namely, you need to know about: Configuring the Factory Handling Validation Errors Configuring the Factory By default, the factory returns a nonvalidating parser that knows nothing about namespaces. To get a validating parser, and/or one that understands namespaces, you configure the factory to set either or both of those options using the command(s) highlighted below: public static void main(String argv[]) { if (argv.length != 1) { ... } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); try { ... Note: JAXP-conformant parsers are not required to support all combinations of those options, even though the reference parser does. If you specify an invalid combination of options, the factory generates a ParserConfigurationException when you attempt to obtain a parser instance. You'll be learning more about how to use namespaces in the last section of the DOM tutorial, Using Namespaces. To complete this section, though, you'll want to learn something about... Handling Validation Errors Remember when you were wading through the SAX tutorial, and all you really wanted to do was construct a DOM? Well, here's when that information begins to pay off. Recall that the default response to a validation error, as dictated by the SAX standard, is to do nothing. The JAXP standard requires throwing SAX exceptions, so you exactly the same error handling mechanisms as you used for a SAX app. In particular, you need to use the DocumentBuilder's setErrorHandler method to supply it with an object that implements the SAX ErrorHandler interface. Note: DocumentBuilder also has a setEntityResolver method you can use The code below uses an anonymous inner class adapter to provide that ErrorHandler. The highlighted code is the part http://java.sun.com/xml/jaxp-1.1/docs/tutorial/dom/1_read.html (5 of 6) [8/22/2001 12:52:25 PM] 1. Reading Data into a DOM that makes sure validation errors generate an exception. builder.setErrorHandler( new org.xml.sax.ErrorHandler() { // ignore fatal errors (an exception is guaranteed) public void fatalError(SAXParseException exception) throws SAXException { } // treat validation errors as fatal public void error(SAXParseException e) throws SAXParseException { throw e; } // dump warnings too public void warning(SAXParseException err) throws SAXParseException { System.out.println("** Warning" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId()); System.out.println(" " + err.getMessage()); } } ); This code uses an anonymous inner class to generate an instance of an object that implements the ErrorHandler interface. Since it has no class name, it's "anonymous". You can think of it as an "ErrorHandler" instance, although technically it's a no-name instance that implements the specified interface. The code is substantially the same as that described the Handling Errors section of the SAX tutorial. For a more background on validation issues, refer to Using the Validating Parser in that part of the tutorial. Looking Ahead In the next section, you'll display the DOM structure in a JTree and begin explore its structure. For example, you'll see how entity references and CDATA sections appear in the DOM. And perhaps most importantly, you'll see how text nodes (which contain the actual data) reside under element nodes in a DOM. Top Contents Index Glossary http://java.sun.com/xml/jaxp-1.1/docs/tutorial/dom/1_read.html (6 of 6) [8/22/2001 12:52:25 PM] 2a. Displaying a DOM Hierarchy Top Contents Index Glossary 2a. Displaying a DOM Hierarchy To create a Document Object Hierarchy (DOM) or manipulate one, it helps Link Summary to have a clear idea of how nodes in a DOM are structured. In this section Exercise Links of the tutorial, you'll expose the internal structure of a DOM. DomEcho02.java Echoing Tree Nodes External Links What you need at this point is a way to expose the nodes in a DOM so can see what it contains. To do that, you'll convert a DOM into a JTreeModel DOM 2 Core Specification and display the full DOM in a JTree. It's going to take a bit of work, but the Understanding the TreeModel end result will be a diagnostic tool you can use in the future, as well as something you can use to learn about DOM structure now. Convert DomEcho to a GUI App Since the DOM is a tree, and the Swing JTree component is all about displaying trees, it makes sense to stuff the DOM into a [ Pobierz całość w formacie PDF ] |
||||
Wszelkie Prawa Zastrzeżone! Lubię Cię. Bardzo. A jeszcze bardziej się cieszę, że mogę Cię lubić. Design by SZABLONY.maniak.pl. | |||||