JAXP « XML « Java Articles

Home
Java Articles
1.Build Deploy
2.Class
3.Core Library
4.Data Types
5.Database JDBC
6.Design
7.Development
8.File Input Output
9.Graphics Desktop
10.J2EE Enterprise
11.J2ME Wireless
12.JVM
13.Language
14.Library Product
15.Network
16.Security
17.SOA Web Services
18.Test
19.Web Development
20.XML
Java Articles » XML » JAXP 
The eXtensible Markup Language (XML) has gained tremendous popularity over recent years because of its ease of use and portability. When coupled with the Java programming language, the end result is a fitting combination of portable data and portable code. Every Java developer that interacts with XML documents in any form, from reading data to performing data transformation, must have a strong understanding of the Java API's for XML Processing (JAXP). Writing XML parser independent code has many benefits; the JAXP API is to XML as the JDBC API is to the relational database. This introductory article will educate developers about the JAXP API, and provide them with a strong understanding of the pluggability layer that will allow their applications to switch between parsers at will.

One of my earlier articles, "Saving Money with Legacy Data," explained how to transform legacy data into XML by using XSLT. I'll use the XML data from that article as the input for this one. We'll also explore Java API for XML Processing (JAXP) parser configuration, in particular taking XML data and transforming it into an HTML-readable format. To do this, I'll make further use of Extensible Stylesheet Language (XSL).

Without SAX, DOM, or another XML parsing API, you cannot parse XML. I have seen many requests for a comparison of SAX, DOM, JDOM, and dom4j to JAXP, but making such comparisons is impossible because the first four APIs serve a completely different purpose from JAXP. SAX, DOM, JDOM, and dom4j all parse XML. JAXP provides a means of getting to these parsers and the data that they expose, but doesn't offer a new way to parse an XML document. Understanding this distinction is critical if you're going to use JAXP correctly. It will also most likely put you miles ahead of many of your fellow XML developers.

JAXP 1.3 completes the mapping by defining the missing types as a part of the Java platform. Table 2 shows the newly defined Java types and their mapping to the XML Schema datatypes.

For several years, the Java API for XML Processing (JAXP) remained a stable, somewhat boring API. That's not a bad thing. Being boring often translates to being reliable -- always a plus for software. But JAXP's dullness has lulled developers away from looking to it for new features. JAXP didn't evolve much from Java 1.3 to 1.4, aside from supporting the newest versions of the SAX and DOM specifications (see Resources). But in Java 5.0 and JAXP 1.3, Sun has added to JAXP significantly. Along with support for XPath, validation is the most notable addition. This article gives you an in-depth look at JAXP 1.3's validation features, implemented in the javax.xml.validation package.

JAXP 1.3, which will be part of J2SE 5 and J2EE 4, is the first major release of this API in over three years. In this pair of articles, we will explore each of the areas of new functionality added to JAXP in this new version.

In this lesson, I will explain default XSLT behavior, and will show you how to write Java code that mimics that behavior. The resulting Java code serves as a skeleton for more advanced transformation programs.

Richard has participated in numerous consulting projects, and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Programming Tutorials, which has gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.

In the previous lesson entitled Java JAXP, Writing Java Code to Emulate an XSLT Transformation, I showed you how to write a Java program that mimics an XSLT transformation for converting an XML file into a text file. I also showed that once you have a library of Java methods that emulate XSLT elements, it is no more difficult to write a Java program to transform an XML document than it is to write an XSL stylesheet to transform the same document.

A DOM parser can be used to create a tree structure in memory that represents an XML document. In Java, that tree structure is encapsulated in an object of the interface type Document. Document and its superinterface Node declare numerous methods that may be used to navigate, extract information from, modify, and otherwise manipulate the DOM tree. As is always the case, classes that implement Document must provide concrete definitions of those methods.

As an example of an advantage provided by XSLT, if it is possible to perform the required transformation using XSLT, that approach will probably require you to write less code than would be required to perform the same transformation by writing a Java program from scratch. However, I will show that once you have a library of Java methods that emulate XSLT elements, it is no more difficult to write a Java program to transform an XML document than it is to write an XSL stylesheet to transform the same document.

Java API for XML Processing (JAXP) 2200 Java API for XML Processing (JAXP), Getting Started 2202 Getting Started with Java JAXP and XSL Transformations (XSLT) 2204 Java JAXP, Exposing a DOM Tree 2206 Java JAXP, Implementing Default XSLT Behavior in Java 2208 Java JAXP, Writing Java Code to Emulate an XSLT Transformation 2210 Java JAXP, Transforming XML to XHTML Links to numerous XML tutorials by Richard G. Baldwin Scalable Vector Graphics (SVG) Scalable Vector Graphics (SVG) 1.1 Specification Adobe SVG Viewer plug-in Create vector graphics in the browser with SVG by Uche Ogbuji SVG Tutorial SVG Basics Miscellaneous High Performance JavaScript Vector Graphics Library at www.walterzorn.com

As the name implies, the Java API for XML Processing (JAXP) is an API provided by Sun designed to help you write programs for processing XML documents. JAXP is very important for many reasons, not the least of which is the fact that it is a critical part of the Java Web Services Developer Pack (Java WSDP). According to Sun,

Java plus XML is a combination of skills that is currently much in demand. For Java programmers that want jump into the XML fray, this article shows you the basics of using the Java API for XML Processing (JAXP).

w__w_w__._j_a__va2___s.___c__o_m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.