Java XML by Example (Part I): JAXP
Author: Darshan Singh (Managing Editor, PerfectXML.com)
Last Updated: July 11, 2003
Java promises "portable code" and XML assures "portable data". These two technologies can
be used together to architect enterprise application that work cross-platform and over the
Internet. Sun Microsystems and various other organizations are continuously working to add
and enhance XML/Web services support in Java.
The goal of this articles series is less talk and more code, with focus on a particular Java XML
technology. This time, we'll focus on Java API for XML Processing (or JAXP).
• Loading an XML Document using DOM: First example illustrates loading an XML
document into a DOM tree and checking if the XML document is well-formed.
• Creating an XML Document using DOM: Second example illustrates using DOM to
create an XML document from scratch and saving it to a disk file. This example
converts a comma separated (CSV) file to an XML document.
• Evaluate XPath Expressions: XML Path Language (or XPath) is a W3C standard that
primarily allows identifying parts of an XML document. Click here for a quick
introduction to XPath.
The third example uses SAXON 6.5.2 alongwith JAXP to load an XML document using
DOM and then evaluate an XPath expression.
• Applying XSLT Stylesheets: XSL Transformations (or XSLT) stylesheets are used to
transform XML documents into any other text format (such as XML, HTML, WML, etc.).
The fourth example accepts names of XML and XSLT files as command line parameters,
applies the transformation and sends the transformed output to the console.
• Calling a .NET Web Service: XML Web services created using ASP.NET support GET
and POST methods of Web service invocation in addition to SOAP. That means, a Web
service method can be called by sending a HTTP GET request or a HTTP POST request,
in addition to SOAP invocation. This example uses SAX to load a remote XML
document - the URL provided in essence is a HTTP GET request to a Web service. This
example applies XSLT stylesheet on the XML returned by the Web service and saves
the transformed HTML into a disk file.
We have used J2SE 1.4.1_02 and SAXON in the following examples; and Eclipse as the IDE
to write and debug these Java console applications.