XML APIs in Java

Source: Internet
Author: User
Tags ldap xslt xslt processor

Let's briefly introduce Java's xml APIs so that you can understand what the abbreviations are. The level is limited.
  
1. JAXP (Java API for XML Parsing)

2. JAXB (Java API for XML Binding)

3. JAXM (Java API for XML Messaging)

4. JAX-RPC (Java API for XML-RPC)

1. JAXP defines a common interface for using DOM, SAX, and XSLT in Java. In this way, you only need to use these universal interfaces in your program, and you do not need to modify the code when you need to change the specific implementation. For example, the XSLT processor you use is too slow. If you want to change one, you don't need to modify your previous code. You just need to modify the relevant configurations of JAXP. (I will introduce it in detail later) as a common interface, JAXP also has the so-called "minimum public Denominator" effect, that is, it supports very limited things. JAXP1.0 supports XML1.0, XML Namespace1.0, SAX1.0, and DOM level 1. JAXP1.1 has added support for SAX2.0, DOM level 2, and javast1.0. Obviously, if you want to use Xalan's XPath-related interfaces, JAXP does not support them, and you can only bind the code to a specific Xalan API.

Although JDOM has not implemented JAXP, JDOM is still very popular because of its simplicity and has become an API officially recommended by JCP. It is also a tree structure of XML, which is much easier to use than the w3c dom standard. The latest version of JDOM has started to use JAXP APIs internally. It will try its best to call JAXP APIs. If not, use its default XML Parser Xerces and XSLT processor Xalan.

2. JAXB defines a bidirectional ing between Java Data Objects and xml structures. In this way, you can easily store a Java object as an xml document, or instantiate a Java object from an xml document. Its structure is like this: first, you must have an xml dtd and binding schema (this is not an xml schema, but an xml document that defines the ing relationship between Java objects and xml structures ), the two files JAXB can generate the Java source file with the same structure as the xml file, after compilation, you can easily obtain the Java classes (that is, the generated classes) that are consistent with the xml structure through the specific xml documents. In turn, the marshalling class can also be obtained.

Its disadvantages are also obvious. Once the xml structure changes, it is necessary to re-write the bindng schema and re-Generate and compile the Java class.

Sun's actions are always slow. Before the introduction of JAXB, there were some frameworks used for xml data binding. Let's take a look at the xml databinding framework that didn't implement JAXB:

I. Castor

Castor not only supports binding to XML, but also supports LDAP ing SQL queries to LDAP objects using OQL and JDO. Unlike JAXB, it only requires xml Schema. Use the xml Schema to generate the corresponding Java source code. After compilation, You can compile alling and unmarshalling.

Ii. Zeus

Compared with Castor and JAXB, Zeus performs more steps in class generation, so it supports various constraints, including DTD, XML Schema, and TREX. However, this project does not seem to have been completed.

Iii. Quick

Quick is also a very flexible framework. You can google it for details.

3. JAXM

JAXM is an API that provides access methods and transmission mechanisms for SOAP communication. Currently, it supports SOAP1.1 specifications and synchronous and asynchronous communication. JAXM defines a large number of services. JAXM implementation products will provide these services so that developers do not have to face complex communication systems. The JAXM architecture includes two important components: JAXM Client and Provider. A Client is usually part of a J2EE web or EJB container to provide the ability of the program you write to access the JAXM service. Provider can be implemented in different ways, and is mainly responsible for sending and receiving SOAP messages. In this way, you can directly use the jaxm api to directly send and receive SOAP messages.

4. JAX-RPC

A JAX-RPC is a Java API called remotely through xml. It is based on the SOAP technology and uses SOAP as the underlying protocol. In this way, developers only have methods, parameters, and return values visible, and the underlying soap communication is hidden, so developers do not need to deal with it directly.

JAXM and JAX-RPC play an important role in Web Services.

Supplement: Steps for finding the specific implementation of the JAXP framework

JDK1.4 comes with the reference implementation of JAXP: DOM of Crimson, SAX Parser, and Xalan XSLT processor.

If you want to replace them with other implementations, you must understand the specific steps of the JAXP framework search implementation:

1. First, the algorithm locates the implemented class through system attributes such as javax. xml. transform. TranformerFactory. You can specify in the command line:

Java-Djavax. xml. transform. TransformerFactory = com. foo. ConcreteTransformer YourApp

ConcreteTransformer is a subclass that implements TransformerFactory. If you use ant, you can specify it in build file.

Similarly, javax.xml.parsers.doc ument. uilderFactory and javax. xml. parsers. SAXBuilderFactory attributes.

2. If the system property is not specified, JAXP will find lib/jaxp In the JRE directory. the properties property file, which is composed of name = value like a common properties file, is assumed to have the following line:

Javax. xml. transform. TransformerFactory = com. foo. ConcreteTransformer

Then JAXP uses the corresponding TransformerFactory implementation.

In a Java program, you can use the following code to obtain the JRE directory:

String javaHomeDir = System. getProperty ("java. home ");

However, it should be noted that if it is used in some ides, IDE will change the value of java. home, such as JBuilder.

3. If jaxp. properties does not exist or there is no corresponding value, JAXP will use the service provision system of the JAR file to locate the correct subclass. To put it simply, you can create a file named javax in the META-INF/services Directory of the jar file. xml. transform. transformerFactory file, which contains only one line: com. foo. concreteTransformer.

4. If no specific implementation is found in the above three steps, JAXP will use the default implementation: Crimson and Xalan.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.