Magic Parser Full Download
XML processing toolkit facilitates parsing and validation
Java technology and XML are arguably the most important programming developments of the last five years. As a result, APIs for working with XML in the Java language have proliferated. The two most popular -- the Document Object Model (DOM) and the Simple API for XML (SAX) -- have generated a tremendous amount of interest, and JDOM and The answer is somewhat complicated.
What is the difference between Setup and Portable versions × Revo Uninstaller Pro Portable is a non-installable version of Revo Uninstaller Pro that does not store any information in Windows Registry and can be used on removable drives. The licensing of Revo Uninstaller Pro Portable is per user, not per computer. In terms of functionality both versions of Revo Uninstaller Pro are identical.
First, the com.sun.xml.tree.XMLDocument
class is not part of JAXP. It is part of Sun's Crimson parser, packaged in earlier versions of JAXP. So the question is misleading from the start. Second, a major purpose of JAXP is to provide vendor independence when dealing with parsers. With JAXP, you can use the same code with Sun's XML parser, Apache's Xerces XML parser, and Oracle's XML parser. Using a Sun-specific class, then, violates the point of using JAXP. Are you starting to see how this subject has gotten muddied? The parser and the API in the JAXP distribution have been lumped together, and some developers mistake classes and features from one as part of the other, and vice versa.
Now that you can see beyond all the confusion, you're ready to move on to some code and concepts.
Starting with SAX
SAX is an event-driven methodology for processing XML. It consists of many callbacks. For example, the startElement()
callback is invoked every time a SAX parser comes across an element's opening tag. The characters()
callback is called for character data, and then endElement()
is called for the element's end tag. Many more callbacks are present for document processing, errors, and other lexical structures. You get the idea. The SAX programmer implements one of the SAX interfaces that defines these callbacks. SAX also provides a class called DefaultHandler
(in the org.xml.sax.helpers
package) that implements all of these callbacks and provides default, empty implementations of all the callback methods. (You'll see that this is important in my discussion of DOM in the next section, Dealing with DOM.) The SAX developer needs only extend this class, then implement methods that require insertion of specific logic. So the key in SAX is to provide code for these various callbacks, then let a parser trigger each of them when appropriate. Here's the typical SAX routine:
- Create a
SAXParser
instance using a specific vendor's parser implementation. - Register callback implementations (by using a class that extends
DefaultHandler
, for example). - Start parsing and sit back as your callback implementations are fired off.
JAXP's SAX component provides a simple means for doing all of this. Without JAXP, a SAX parser instance either must be instantiated directly from a vendor class (such as org.apache.xerces.parsers.SAXParser
), or it must use a SAX helper class called XMLReaderFactory
(also in the org.xml.sax.helpers
package). The problem with the first methodology is obvious: It isn't vendor neutral. The problem with the second is that the factory requires, as an argument, the String
name of the parser class to use (that Apache class, org.apache.xerces.parsers.SAXParser
, again). You can change the parser by passing in a different parser class as a String
. With this approach, if you change the parser name, you won't need to change any import statements, but you will still need to recompile the class. This is obviously not a best-case solution. It would be much easier to be able to change parsers without recompiling the class.
JAXP offers that better alternative: It lets you provide a parser as a Java system property. Of course, when you download a distribution from Sun, you get a JAXP implementation that uses Sun's version of Xerces. Changing the parser -- say, to Oracle's parser -- requires that you change a classpath setting, moving from one parser implementation to another, but it does not require code recompilation. And this is the magic -- the abstraction -- that JAXP is all about.
A look at the SAX parser factory
The JAXP SAXParserFactory
class is the key to being able to change parser implementations easily. You must create a new instance of this class (which I'll look at in a moment). After the new instance is created, the factory provides a method for obtaining a SAX-capable parser. Behind the scenes, the JAXP implementation takes care of the vendor-dependent code, keeping your code happily unpolluted. This factory has some other nice features, as well.
In addition to the basic job of creating instances of SAX parsers, the factory lets you set configuration options. These options affect all parser instances obtained through the factory. The two most commonly used options available in JAXP 1.3 are to set namespace awareness with setNamespaceAware(boolean awareness)
, and to turn on DTD validation with setValidating(boolean validating)
. Remember that once these options are set, they affect all instances obtained from the factory after the method invocation. Symbol scanner ds6707 driver download.
Once you have set up the factory, invoking newSAXParser()
returns a ready-to-use instance of the JAXP SAXParser
class. This class wraps an underlying SAX parser (an instance of the SAX class org.xml.sax.XMLReader
). It also protects you from using any vendor-specific additions to the parser class. (Remember the discussion about the XmlDocument
class earlier in this article?) This class allows actual parsing behavior to be kicked off. Listing 1 shows how you can create, configure, and use a SAX factory:
Listing 1. Using the SAXParserFactory
In Listing 1, you can see that two JAXP-specific problems can occur in using the factory: the inability to obtain or configure a SAX factory, and the inability to configure a SAX parser. The first of these problems, represented by a FactoryConfigurationError
, usually occurs when the parser specified in a JAXP implementation or system property cannot be obtained. The second problem, represented by a ParserConfigurationException
, occurs when a requested feature is not available in the parser being used. Both are easy to deal with and shouldn't pose any difficulty when using JAXP. In fact, you might want to write code that attempts to set several features and gracefully handles situations where a certain feature isn't available.
A SAXParser
instance is obtained once you get the factory, turn off namespace support, and turn on validation; then parsing begins. The SAX parser's parse()
method takes an instance of the SAX HandlerBase
helper class that I mentioned earlier, which your custom handler class extends. See the code distribution to view the implementation of this class with the complete Java listing (see Download). You also pass in the File
to parse. However, the SAXParser
class contains much more than this single method.
Working with the SAX parser
Once you have an instance of the SAXParser
class, you can do a lot more than just pass it a File
to parse. Because of the way components in large applications communicate, it's not always safe to assume that the creator of an object instance is its user. One component might create the SAXParser
instance, while another component (perhaps coded by another developer) might need to use that same instance. For this reason, JAXP provides methods to determine the parser's settings. For example, you can use isValidating()
to determine if the parser will -- or will not -- perform validation, and isNamespaceAware()
to see if the parser can process namespaces in an XML document. These methods can give you information about what the parser can do, but users with just a SAXParser
instance -- and not the SAXParserFactory
itself -- do not have the means to change these features. You must do this at the parser factory level.
You also have a variety of ways to request parsing of a document. Instead of just accepting a File
and a SAX DefaultHandler
instance, the SAXParser
's parse()
method can also accept a SAX InputSource
, a Java InputStream
, or a URL
in String form, all with a DefaultHandler
instance. So you can still parse documents wrapped in various forms.
Finally, you can obtain the underlying SAX parser (an instance of org.xml.sax.XMLReader
) and use it directly through the SAXParser
's getXMLReader()
method. Once you get this underlying instance, the usual SAX methods are available. Listing 2 shows examples of the various uses of the SAXParser
class, the core class in JAXP for SAX parsing:
Listing 2. Using the JAXP SAXParser
class
Up to this point, I've talked a lot about SAX, but I haven't unveiled anything remarkable or surprising. JAXP's added functionality is fairly minor, especially where SAX is involved. This minimal functionality makes your code more portable and lets other developers use it, either freely or commercially, with any SAX-compliant XML parser. That's it. There's nothing more to using SAX with JAXP. If you already know SAX, you're about 98 percent of the way there. You just need to learn two new classes and a couple of Java exceptions, and you're ready to roll. If you've never used SAX, it's easy enough to start now.
Dealing with DOM
If you think you need to take a break to gear up for the challenge of DOM, you can save yourself some rest. Using DOM with JAXP is nearly identical to using it with SAX; all you do is change two class names and a return type, and you are pretty much there. If you understand how SAX works and what DOM is, you won't have any problem.
The primary difference between DOM and SAX is the structures of the APIs themselves. SAX consists of an event-based set of callbacks, while DOM has an in-memory tree structure. With SAX, there's never a data structure to work on (unless the developer creates one manually). SAX, therefore, doesn't give you the ability to modify an XML document. DOM does provide this functionality. The org.w3c.dom.Document
class represents an XML document and is made up of DOM nodes that represent the elements, attributes, and other XML constructs. So JAXP doesn't need to fire SAX callbacks; it's responsible only for returning a DOM Document
object from parsing.
Msxml Parser Download
A look at the DOM parser factory
With this basic understanding of DOM and the differences between DOM and SAX, you don't need to know much more. The code in Listing 3 looks remarkably similar to the SAX code in Listing 1. First, a DocumentBuilderFactory
is obtained (in the same way that SAXParserFactory
was in Listing 1). Then the factory is configured to handle validation and namespaces (in the same way that it was in SAX). Next, a DocumentBuilder
instance, the analog to SAXParser
, is retrieved from the factory (in the same way . . . you get the idea). Parsing can then occur, and the resultant DOM Document
object is handed off to a method that prints the DOM tree:
Listing 3. Using the DocumentBuilderFactory
Two problems can arise with this code (as with SAX in JAXP): a FactoryConfigurationError
and a ParserConfigurationException
. The cause of each is the same as it is with SAX. Either a problem is present in the implementation classes (resulting in a FactoryConfigurationError
), or the parser provided doesn't support the requested features (resulting in a ParserConfigurationException
). The only difference between DOM and SAX in this respect is that with DOM you substitute DocumentBuilderFactory
for SAXParserFactory
, and DocumentBuilder
for SAXParser
. It's that simple. (You can view the complete code listing, which includes the method used to print out the DOM tree; see Download.)
Working with the DOM parser
Once you have a DOM factory, you can obtain a DocumentBuilder
instance. The methods available to a DocumentBuilder
instance are very similar to those available to its SAX counterpart. The major difference is that variations of the parse()
method do not take an instance of the SAX DefaultHandler
class. Instead they return a DOM Document
instance representing the XML document that was parsed. The only other difference is that two methods are provided for SAX-like functionality:
setErrorHandler()
, which takes a SAXErrorHandler
implementation to handle problems that might arise in parsingsetEntityResolver()
, which takes a SAXEntityResolver
implementation to handle entity resolution
Listing 4 shows examples of these methods in action:
Listing 4. Using the JAXP DocumentBuilder
class
If you're a little bored reading this section on DOM, you're not alone; I found it a little boring to write because applying what you've learned about SAX to DOM is so straightforward.
Performing validation
In Java 5.0 (and JAXP 1.3), JAXP introduces a new way to validate documents. Instead of simply using the setValidating()
method on a SAX or DOM factory, validation is broken out into several classes within the new javax.xml.validation
package. I would need more space than I have in this article to detail all the nuances of validation -- including W3C XML Schema, DTDs, RELAX NG schemas, and other constraint models -- but if you already have some constraints, it's pretty easy to use the new validation model and ensure that your document matches up with them.
First, convert your constraint model -- presumably a file on disk somewhere -- into a format that JAXP can use. Load the file into a Source
instance. (I'll cover Source
in more detail in Part 2; for now, just know that it represents a document somewhere, on disk, as a DOM Document
or just about anything else.) Then, create a SchemaFactory
and load the schema using SchemaFactory.newSchema(Source)
, which returns a new Schema
object. Finally, with this Schema
object, create a new Validator
object with Schema.newValidator()
. Listing 5 should make everything I've just said much clearer:
Listing 5. Using the JAXP validation framework
This is pretty straightforward once you get the hang of it. Type this code in yourself, or check out the full listing (see Download).
Changing the parser
It's easy to change out the parser that the JAXP factory classes use. Changing the parser actually means changing the parser factory, because all SAXParser
and DocumentBuilder
instances come from these factories. The factories determine which parser is loaded, so it's the factories that you must change. To change the implementation of the SAXParserFactory
interface, set the Java system property javax.xml.parsers.SAXParserFactory
. If this property isn't defined, then the default implementation (whatever parser your vendor specified) is returned. The same principle applies for the DocumentBuilderFactory
implementation you use. In this case, the javax.xml.parsers.DocumentBuilderFactory
system property is queried.
Log Parser Download
Summary
Having read this article, you've seen almost the entire scope of JAXP:
- Provide hooks into SAX
- Provide hooks into DOM
- Allow the parser to easily be changed out
To understand JAXP's parsing and validation features, you'll wade through very little tricky material. The most difficult parts of putting JAXP to work are changing a system property, setting validation through a factory instead of a parser or builder, and getting clear on what JAXP isn't. JAXP provides a helpful pluggability layer over two popular Java and XML APIs. It makes your code vendor neutral and lets you to change from parser to parser without ever recompiling your parsing code. So download JAXP and go to it! Part 2 will show you how JAXP can help you transform XML documents.
Downloadable resources
- Sample code for All about JAXP (x-jaxp-all-about.zip 5 KB)
Related topics
- Learn more about JAXP at Sun's Java and XML headquarters.
- If you're new to Java programming, you can get JAXP along with a complete JDK by downloading Java 5.0.
- For an in-depth look at the new features in JAXP 1.3, read the two-part developerWorks series 'What's new in JAXP 1.3?':
- Part 1 (November 2004) provides a brief overview of the JAXP specification, gives details of the modifications to the javax.xml.parsers package, and describes a powerful schema caching and validation framework.
- Find out more about the APIs under the covers of JAXP. Start with SAX 2 for Java at the SAX Web site, and then take a look at DOM at the W3C Web site.
- Download the Apache Xerces parser in its JDK 5.0 implementation.
- Read 'Achieving vendor independence with SAX' (developerWorks, March 2001) to learn how to use SAX and a SAX helper class to achieve vendor independence in your SAX-based applications.
- Learn more about JDOM, an open source toolkit that provides a way to represent XML documents in the Java language for easy and efficient reading, writing, and manipulation.
- Check out dom4j, an open source library for working with XML, XPath, and XSLT on the Java platform.
- Read Brett McLaughlin's book Java & XML (O'Reilly & Associates, 2001), which explains how Java programmers can use XML to build Web-based enterprise applications.
- Find out how you can become an IBM Certified Developer.
Comments
Sign in or register to add and subscribe to comments.
Pages: Go to 1 2 3FreewareMaciPhone | Size | Type | Added | |
| 39.0 KB | freeware | 01 March, 2013 | |
| 48.0 KB | shareware | 30 January, 2013 | |
| shareware | 13 February, 2009 | ||
| 154.6 KB | freeware | 03 November, 2012 | |
| 2.0 KB | freeware | 16 November, 2012 | |
| 3.0 KB | freeware | 26 December, 2012 | |
| 186.0 KB | freeware | 10 April, 2010 | |
| 243.9 KB | freeware | 27 October, 2012 | |
| 10.0 MB | freeware | 09 December, 2012 | |
| 4.3 MB | shareware | 05 March, 2008 | |
| 1.8 MB | shareware | 05 March, 2008 | |
| 4.8 MB | shareware | 05 March, 2008 | |
| 1.4 MB | shareware | 05 March, 2008 | |
| 12.1 MB | shareware | 05 March, 2008 | |
| 122.8 KB | shareware | 05 March, 2008 | |
Pages: Go to 1 2 3 |
- HelpSmith
2024 downloads - Web Log Explorer Lite
408 downloads - Website Watchman
40 downloads - AnyChart JS Charts and Dashboards
328 downloads - dbExpress Driver for SQLite
103 downloads - mightymacros Excel Utilities
1 downloads - Total CSV Converter
2 downloads - ReliefJet Essentials for Outlook
100 downloads - DataNumen Office Repair
140 downloads - TimeBillingWindow
160 downloads - PCX To JPG Converter Software
1 downloads - Brave Alchemist
1 downloads - Midi Tab Player
15 downloads - Aiseesoft Video Editor
9 downloads - Aiseesoft Video Converter Ultimate
16 downloads
- FullRecall
3888 downloads - Outlook Export Wizard
3592 downloads - SugarSync Manager
4621 downloads - Belarc Advisor
3281 downloads - Cobian Backup
3433 downloads - Samsung Kies
3496 downloads - Camfrog
5436 downloads - EaseUS Partition Master
3315 downloads - SUPERAntiSpyware
4019 downloads - PeerBlock
3878 downloads - SUMo
3602 downloads - Turbo Pascal
4045 downloads