JAXP (1)

xiaoxiao2021-03-06  106

This is the articles detaking JAXP, Sun's Java API for XML, helping to release doubts about JAXP nature and service purposes. This article explains the basic concepts of JAXP, and the demo XML syntax analysis requires JAXP and display how to easily change the syntax analyzer used by JAXP. This article further describes two popular SAX and DOMs related to JAVA and XML APIs related to JAXP.

Java and XML created news in every technical field, and for software developers, it seems to be the most important development in 1999 and 2000. As a result, the number of Java and XML APIs surged. Two of the most popular DOMs and SAX have also attracted great interest, while JDOM and data binding API come one after another. Just thorough understanding one or both of these technologies is a daunting task, and correctly uses all of these technologies will make you an expert. But last year, another API left a deep impression, it was Sun's Java API for XML, usually known as JAXP. This progress is not surprising if it is considered that Sun has an XML product on its platform. Surprising is the lack of JAXP understanding. Most developers using its developers have errors in the concept of this API.

What is JAXP? This article assumes that you have basic knowledge of SAX and DOM. There is no sufficient space here to explain SAX, DOM, and JAXP. If you are a newcomer in XML syntax analysis, you may have to read SAX and DOM through online resources, or browse my book. (Reference Resources There is a link to the API and my book.) After obtaining basic knowledge, it will be better.

Is API or abstract? Introducing some basic concepts before explaining the code. Strictly speaking, JAXP is an API, but it is called an abstract layer more accurate. It does not provide new ways to process XML, not complement SAX or DOM, nor does it provide new features to Java and XML processing. (If you understand this, this article is just right!) It is only easier to handle some difficult tasks through the DOM and SAX. If you encounter a vendor-specific task when using the DOM and SAX API, it also makes it possible to handle these tasks by independent vendors.

Although all of these features are to be described separately, truly need to master: JAXP does not provide grammar analysis function! There is no SAX, DOM or another XML syntax analysis, and the XML syntax cannot be analyzed. Many people have let me compare DOM, SAX or JDOM with JAXP. However, it is impossible to perform these comparisons because the top three APIs are completely different from JAXP. SAX, DOM, and JDOM are analyzed XML syntax. JAXP provides a way to reach these grammar analyzers and results. It does not provide a new way to analyze document syntax. If you want to use JAXP correctly, you must figure out this. This will enable you a big chance than other XML developers.

If you still doubt (or think of me, please download JAXP distribution from Sun's Web Site (see Refigu), then you will know what basic jaxp is. There are only six classes included in JAR (JAXP.jar)! How many times will this API? All of these classes (part of the javax.xml.parsers package are located on the existing syntax analyzer. Two of these classes are also used for error handling. JAXP is much simpler than people think. So why is it still confused?

Sun's JAXP and Sun Syntax Analyzer JAXP downloads the syntaxial analyzer when SUN is downloaded. All PARSER operators are located in the Parser.jar file as part of the com.sun.xml.parser package and the related subcaps. It should be known that the grammar analyzer (code name crimson) is not part of JAXP itself. It is part of the JAXP version, but is not part of the JAXP API. Is it confusing? A little. For this way, think about: JDOM is provided with the Apache Xerces Grammar Analyzer. The syntax analyzer is not part of the JDOM, but is used by JDOM, so it includes it to ensure that JDOM can be used separately. This is also the case, but it is not as good as JDOM: JAXP is provided with Sun's Syntax Analyzer so that you can use it immediately. However, many people use the class included in the SUN as part of the JAXP API. For example, a common problem in the newsgroup is: "How to use the XMLDocument class in JAXP? What is its purpose?" This answer can be more complicated. First, com.sun.xml.tree.xmldocument classes are not part of JAXP. It is part of the Sun Syntax Analyzer. Therefore, this problem gives people a misleading from the beginning. Second, JAXP's entire significance is to provide supplier independence when processing the syntax analyzer. The same code to use JAXP can be used with the Sun's XML syntax analyzer, Apache's Xerces XML Syntax, and Oracle's XML Syntax Analyzer. The use of Sun-specific classes is a bad idea. This is from the entire meaning of JAXP. What is the confusion of this problem now? The API in the grammatical analyzer and JAXP release (at least sun version) is mixed, and the developer will use one of the classes and characteristics as another, and vice versa.

Old and new JAXP, and finally need to point out that there are some defects using JAXP. For example, JAXP only supports SAX 1.0 and DOM first layer specifications. SAX 2.0 is completed from May 2000, and the DOM second layer specification support is even longer in most syntax analyzers. The second layer of DOM has not been completed, but it is really stable for production. The new version of these two API has a significant improvement, the most obvious is support for the XML namespace. This support also allows "XML Schema Confirmation", this related to XML is another popular technology. Equipage said that when JAXP issued 1.0 final release, SAX 2.0 and DOM first-level specification have not been completed. However, since these new versions are not included, it is really inconvenient for developers.

You can also use JAXP, but you can also wait for JAXP 1.1, which supports SAX 2.0 and DOM secondary specifications. Otherwise, it will be found that the advantages provided by JAXP are at the expense of features in SAX and DOM's latest versions, and make applications more difficult to encode. Whether you wait for the next JAXP release, pay attention to this problem. If you use the JAXP with the grammar analyzer, the DOM and SAX version supported by the syntax analyzer are high than JAXP support, and there may be path problems. So, pay attention to it in advance, and once there is JAXP 1.1, upgrade immediately. After basically understanding JAXP, let's take a look at the JAXP-dependent API: SAX and DOM.

From SAX Start SAX (Simple API for XML) is an event-driven method for processing XML. It is basically consisting of many recovery functions. For example, whenever the SAX Syntax Analyzer encounters the start tag of an element, call startElement (). For a string, the characters () callback function will be called, then endElement () is called at the element end tag. There are also a lot of backup functions for document processing, errors, and other vocabulary structures. Now I know what is going on. SAX programmer implements a SAX interface that defines these callback functions. SAX also implements a class named Handlerbase that implements all of these callback functions and provides the default empty implementation of all of these callback methods. (Mention this is because it is very important in the DOM mentioned later.) SAX developers only need to extend this class, and then implement methods that need to be inserted into specific logic. Therefore, the key to SAX is to provide code for these different callback functions, and then allow the syntax analyzer to trigger each of these callback functions when appropriate. Therefore, the typical SAX process is as follows:

Create a SAXPARSER instance with a specific supplier's grammar analyzer

Register a callback implementation (for example, by using the class extension Handlerbase)

Start the grammatical analysis, then wait while triggering the callback implementation

JAXP's SAX component provides a simple way to perform all these steps. If there is no JAXP, the SAX Syntax Analyzer is instantiated directly from the vendor class (such as org.apache.xerces.parsers.saxparser) or must use the helper class named ParserFactory. The first method is obvious: not independent of the supplier. The problem with the second method is that the class is a self-variable, that is, the character string name of the syntax analyzer class to use (or that apache org.apache.xerces.Parsers.saXParser). The syntax analyzer can be changed by passing different grammar analyzers as String. Using this method does not have to change any Import statement, but still recompile the class. This is obviously not the best solution. If you can change the syntax analyzer without recoiling classes, it may be much more simple, is this?

JAXP provides a better alternative: it allows the syntax analyzer as a Java system properties. Of course, when downloading versions from the Sun, a JAXP implementation using the Sun Syntax Analyzer will be obtained. You can download the same JAXP interface to build its implementation from the Apache XML Web site. So (no matter which case), change the syntax analyzer that is being used needs to change the classpath setting, which is changed from one syntax analyzer to another, but does not require recompiling code. This is the magic of JAXP, or abstraction.

The SAX Syntax Analyzer is a JAXP SAXPARSERFACTORY class is the key to easily changing the syntax analysis. New instances of this class must be created (waiting for a while). After creating a new instance, the class is provided with a method to get a syntax analyzer that supports SAX. Inside, JAXP implementation depends on the supplier's code, so that your code is not affected. This type of factory also provides some other excellent features.

In addition to the basic work of creating a SAX Syntax Analyzer instance, the class is also allowed to set the configuration options. These options affect all grammar analyzer instances obtained by the class factory. The two available functions in JAXP 1.0 are setting namespace sensitivity (SETNAMESPACEAWARE (Boolean Awareness), and open confirmation (SETVALIDATING). Remember, once these options are set, after calling the method, they will affect all instances obtained from the class factory. After setting the class factory, call NewsaXParser () will return a JAXP SaxParser class instance available at any time. This class encapsulates a lower SAX syntax analyzer (instance of SAX class org.xml.sax.parser). It also prevents adding any vendor-specific additional features to the syntax analyzer class. (Remember the previous discussion of XMLDocument?) This class can begin practical syntax analysis. The following list shows how to create, configure, and use SAX class.

Listing 1. Using SaxParserFactory

Import java.io.file;

Import java.io.ioException;

Import Java.io.OutputStreamwriter;

Import java.io.writer;

// jaxp

Import javax.xml.parsers.FactoryConfigurationError;

Import javax.xml.parsers.ParserConfigurationException;

Import javax.xml.parsers.saxparserfactory;

Import javax.xml.parsers.saxparser;

// SAX

Import org.xml.sax.attributelist;

Import org.xml.sax.handlerbase;

Import org.xml.sax.saxexception;

Public class testsaxparsing {

Public static void main (String [] args) {

Try {

IF (args.length! = 1) {

System.err.println ("USAGE: Java TestsaXPARSING [FileName]");

System.exit (1);

}

// Get SAX Syntax Analytics Factory

SAXPARSERFAACTORY FACTORY = SAXPARSERFACTORY.NEWINSTANCE ();

/ / Set the setting namespace sensitivity option, turn off the confirmation option

Factory.SetValidating (TRUE);

Factory.setnamespaceaware (false);

SAXPARSER PARSER = factory.newsaxparser ();

Parser.Parse (New file (args [0]), new myHandler ());

} catch (ParserConfigurationException E) {

System.out.println ("The Underlying Parser Does Not Support"

"The Requested Features.");

} catch (factoryconfiguration error e) {

System.out.Println ("Error Occurred Obtaining Sax Parser Factory.");

} catch (exception e) {

E.PrintStackTrace ();

}

}

}

Class MyHandler Extends Handlerbase {// SAX callback function implemented by DocumentHandler, Errorhandler, etc.

}

Note that in this code, two JAXP-specific issues may occur when using the class mill: unable to configure the SAX class, and the SAX syntax analyzer cannot be configured. The first problem FactoryConfigurationError is usually occurring when the syntax analyzer or system properties specified in the JAXP implementation cannot be obtained. The second problem ParserConfigurationException occurs when the characteristics in the syntax analyzer being used is not available. These two issues are easy to handle, and should not cause any difficulties in the use of JAXP.

After obtaining a class, close the namespace and open "confirm", you will get SAXPARSER, then start syntax analysis. Note that the sex syntax analyzer's PARSE () method obtains an instance of the SAX HandlerBase class mentioned earlier. (You can view the implementation of this class through a complete Java list.) Also passed the file you want to perform syntax analysis. However, the SAXPARSER is far more than this method.

After using the SAX Syntax Analyzer to get an instance of the SAXPARSER class, you can use it more things except for grammatical analysis to the syntax analyzer. Due to the communication mode between application components in large applications, "object instance creators are their users" such that they are not always secure. In other words, a component may create an SAXPARSER instance, and another component (possibly by another developer) may need to use that instance. For this reason, some methods are provided to determine the settings of the grammar analyzer. Two ways to perform this task are isvalidating (), which notifies the call: Syntax Analyzer, or do not perform "confirm", and isnamespaceaware (), it returns an indication, indicating that the syntax analyzer can or cannot process XML documents The namespace in the middle. Although these methods provide information about the grammar analyzer to perform function, these features cannot be changed. This must be performed at the grammar analyzer factory level.

In addition, there are a variety of ways to request grammar analysis of documents. In addition to only the File and Sax HandlerBase instances, SaxParser's PARSE () method can also accept SAX INPUTSOURCE, Java InputStream, or URL in String, all of which are provided with the HandlerBase instance. Therefore, different types of input documents can be handled in different ways of grammatical analysis.

Finally, you can use the SAXPARSER's getParser () method to obtain and use the sub-SAX Syntax Analyzer (Org.xml.Sax.Parser's instance). Upon obtaining this underlying instance, a usual SAX method can be obtained. The next list shows various examples of the use of the SAXPARSER class (the core class of SAX syntax analysis in JAXP).

Listing 2. Using JAXP SAXPARSER

// A instance of SAXP

SAXPARSER SAXPARSER = SAXFACTORY.NEWSAXPARSER ();

/ / View whether the validate option is supported

Boolean isvalidating = saxparser.issalidating ();

/ / View Whether to support the Namespace option

Boolean isnamespaceaware = saxparser.isnamespaceaware ();

// Use a File and an instance of Sax Handlerbase to make a variety of forms of syntax analysis SAXPARSER.PARSE (New File (Args [0]), MyHandlerBaseInstance;

// Use a SAX INPUTSOURCE instance and a SAX HandlerBase instance

SAXPARSER.PARSE (MySaxInputSource, MyHandlerBaseInstance);

// Use an InputStream instance and a SAX HandlerBase instance

SAXPARSER.PARSE (MyInputStream, MyHandlerBaseInstance);

// Use a URI and a SAX HandlerBase instance

SAXPARSER.PARSE ("http://www.newinstance.com/xml/doc.xml", myhandlerbaseinstance);

/ / Get the underlying (package) SAX grammar analyzer

Org.xml.sax.Parser Parser = SaxParser.getParser ();

// Using the underlying grammar analyzer

Parser.setContentHandler (MyContentHandlerInstance);

Parser.setdlerHandler (MyErrorhandlerInstance);

Parser.parse (new org.xml.sax.inputsource (args [0]));

So far, there is a lot of SAX, but there is no unusual or amazing thing. In fact, JAXP has little features, especially when SAX is involved. This is very good because there are minimal functionality means being more portable, and can be used by other developers with any XML grammar analyzer with SAX, whether it is free (by open source, hope) or Through the business pathway. That's it. Use SAX in JAXP without more things. If you already know SAX, then about 98% of the content is now. Just learn two new categories and two Java exceptions, you can start. If you have never used SAX, it is also very simple and you can start now.

Treat DOM If you want to rest to greet the DOM challenge, then take a break. The process of using the DOM in JAXP is almost the same as SAX, and all of the you want to do just change two class names and a return type, which is almost. If you understand what SAX works and what DOM is, there will be no problem.

The main differences between DOM and SAX are their API structure. SAX contains an event-based callback function set, while the DOM has a tree structure in memory. In other words, in SAX, the data structure is not required (unless the developer is manually created). Therefore, SAX does not provide the function of modifying the XML document. And DOM just provides this type of functionality. Org.w

3C

.dom.Document class represents an XML document that consists of a DOM node representing elements, attributes, and other XML structures. So, JAXP does not need to trigger SAX callbacks, which is only responsible for returning a Dom Document object from grammatical analysis.

DOM Syntax Analytics Factory Basic understanding Dom and Dom and SAX are not good. The following code looks similar to the SAX code. First, obtain DocumentBuilderFactory (same as the way in SAX). The configuration class is then handled to handle confirmation and namespace (same as the way in SAX). Next, retrieve DocumentBuilder from the class factory (similar to SAXPARSER) (same as the way in SAX..., You know). Then, grammatical analysis can be performed, and the resulting Dom Document object is passed to the method of printing the DOM tree. Listing 3. Using Document Builder

Import java.io.file;

Import java.io.ioException;

Import Java.io.OutputStreamwriter;

Import java.io.writer;

// jaxp

Import javax.xml.parsers.FactoryConfigurationError;

Import javax.xml.parsers.ParserConfigurationException;

Import javax.xml.parsers.documentBuilderFactory;

Import javax.xml.parsers.documentbuilder;

// DOM

Import Org.w

3C

. Dom. Document;

Import Org.w

3C

.dom.documenttype;

Import Org.w

3C

.dom.namednodemap;

Import Org.w

3C

. Dom.Node;

Import Org.w

3C

. Dom.nodelist;

Public class testDomparsing {

Public static void main (String [] args) {

Try {

IF (args.length! = 1) {

System.err.Println ("Usage: Java TestDomparsing");

System.exit (1);

}

// Get Document Builder Factory

DocumentBuilderFactory Factory = DocumentBuilderFactory.newinstance ();

// Open the confirmation option to turn off the namespace sensitivity option.

Factory.SetValidating (TRUE);

Factory.setnamespaceaware (false);

DocumentBuilder Builder = Factory.NewDocumentBuilder ();

Document doc = builder.parse (new file (args [0]));

/ / Print a document from the number of DOM and add an initial space

PrintNode (DOC, ");

/ / You can also modify the DOM document here.

} catch (ParserConfigurationException E) {

System.out.println ("The Underlying Parser Does Not Support The Requested Features.");

} catch (factoryconfiguration error e) {

System.out.println ("Error Occurred Obtaining Document Builder Factory.");

} catch (exception e) {E.PrintStackTrace ();

}

}

Private static void printnode (node ​​node, string indent) {

// Print the DOM tree

}

Two different problems may occur in this code (similar to SAX in JAXP): FactoryConfigurationError and ParserConfigurationException. Every reason is the same as in SAX. It is not a problem in the implementationConfigurationError, that is, the grammatical analyzer does not support requests (PARSERCONFIGURATIONEXCEPTION). The only difference between DOM and SAX is that in the DOM, use DocumentBuilderFactory to replace SAXPARSERFAACTORY, replace SAXPARSER with DocumentBuilder. It's that simple! (You can view the full code list, which includes methods for printing the DOM tree.)

After using the DOM syntax, you can get the DocumentBuilder instance after you have a DOM class. The method of the DocumentBuilder instance can be used very similar to SAX. The main difference is that variants of PARSE () do not require examples of the HandlerBase class. They return the Dom Document instance of the XML document after the syntax analysis. Another unique difference is that two methods are provided to the SAX function: SAX Errorhandler implementation to handle syntax's seterrorhandler (), and SAX EntityResolver implementation to handle SetentityResolver ( ). If you are not familiar with these concepts, you need to learn SAX through online or in my book. The following list shows an example of using these methods.

Listing 4. Using JAXD DocumentBuilder

// Get a DocumentBuilder instance

DocumentBuilder Builder = BuilderFactory.NewDocumentBuilder ();

/ / View whether the validate option is supported

Boolean isvalidating = builder.issalidating ();

/ / View Whether to support the Namespace option

Boolean isnamespaceaware = builder.isnamespaceaware ();

// Set a SAX Errorhandler

Builder.setdlerHandler (MyErrorhandlerImpl);

// Set a SAX EntityResolver

Builder.sentityResolver (MyentityResolver);

// Use a variety of ways to grammar analysis of File

Document doc = builder.parse (new file (args [0]));

// Use SAX InputSource

Document doc = builder.parse (mysaxinputsource);

// Apply InputStream

Document doc = builder.parse (MyInputStream, MyHandlerBaseInstance);

// Use the URI

Document doc = builder.parse ("http://www.newinstance.com/xml/doc.xml");

Is it a bit troubled? There is more than one of this idea, and writing the DOM code is a bit trouble because it is directly acquired SAX knowledge and then uses it for DOM. Therefore, with friends, colleagues bet, saying that using JAXP is just a snatch. Changing the Syntax Analyzer Last Theme is the ability to easily change the syntax analyzer used by JAXP. Changing the syntax analyzer used by JAXP actually changes the class factory because all SaxParser and DocumentBuilder instances come from these classes. Since it is determined which grammar analyzer is a class, therefore, you must change the class. You can change the SAXPARSerFactory interface implementation to use by setting Java System Properties Javax.xml.Parsers.SAXPARSerFactory. If this property is not defined, returns the default implementation (any of the syntax analyzer specified by the supplier). The same principle applies to DocumentBuilderFactory implementation. In this case, Javax.xml.Parsers.DocumentBuilderFactory system properties will be queried. It's so simple, we have already learned! This is all SAXP: Providing the hook to SAX, providing the hook to the DOM, and allowing the syntax analyzer to be easily changed.

Conclusion As you can see, there is not much complicated thing. To change the system properties, through the class, not the grammar analyzer or builder to set "confirm", and how JAXP is not actually what people usually think, these are the most difficult part of using JAXP. In addition to the support of SAX 2.0 and DOM second-level specification, JAXP provides a helpful insertable layer on both popular Java and XML APIs. It makes the code independently of the supplier and allows the syntax analyzer to change the syntax analysis code. So download JAXP from Sun, Apache XML, or other convenience, and use it! Continue to pay attention to JAXP 1.1 and increase support for SAX 2 and DOM 2, XSLT and more. You will get first-hand news here, so please pay attention to developerWorks.

转载请注明原文地址:https://www.9cbs.com/read-98019.html

New Post(0)