Primary JAXP interfaces to the DOM API in Java

Generator QR Code in Java Primary JAXP interfaces to the DOM API

Table 2.4 Primary JAXP interfaces to the DOM API
QR-Code Creator In Java
Using Barcode drawer for Java Control to generate, create QR Code ISO/IEC18004 image in Java applications.
www.OnBarcode.com
QR Code Scanner In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
JAXP class or interface
Print GS1 DataBar Limited In Java
Using Barcode generation for Java Control to generate, create GS1 DataBar Limited image in Java applications.
www.OnBarcode.com
Create Barcode In Java
Using Barcode creation for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
javax.xml.parsers.DocumentBuilderFactory
Create GS1 - 13 In Java
Using Barcode generation for Java Control to generate, create European Article Number 13 image in Java applications.
www.OnBarcode.com
Generate UPC-A Supplement 2 In Java
Using Barcode maker for Java Control to generate, create GTIN - 12 image in Java applications.
www.OnBarcode.com
Description
Paint PDF417 In Java
Using Barcode creator for Java Control to generate, create PDF 417 image in Java applications.
www.OnBarcode.com
Draw Identcode In Java
Using Barcode printer for Java Control to generate, create Identcode image in Java applications.
www.OnBarcode.com
Locates a DocumentBuilderFactory implementation class and instantiates it. The implementation class in turn provides DocumentBuilder implementations. Interface to the underlying DOM builder.
QR Code Generator In Visual Studio .NET
Using Barcode creation for ASP.NET Control to generate, create QR image in ASP.NET applications.
www.OnBarcode.com
QR Creator In Objective-C
Using Barcode generator for iPad Control to generate, create QR Code 2d barcode image in iPad applications.
www.OnBarcode.com
javax.xml.parsers.DocumentBuilder
Denso QR Bar Code Reader In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Generating EAN 13 In Visual C#.NET
Using Barcode encoder for .NET Control to generate, create UPC - 13 image in VS .NET applications.
www.OnBarcode.com
Since our product catalog document is very short, there is no danger in reading it in via DOM. The code to do so is given in listing 2.8. You can see that the general steps of obtaining a parser from JAXP and invoking it on a document are the same. The primary difference is the absence of the SAX event handler. Note also that the parser returns a pointer to the DOM in memory after parsing. Using the other DOM API classes in the org.w3c.dom package, you could traverse the DOM in your code and visit each product in the catalog. We leave that as an exercise for the reader.
Draw USS-128 In VS .NET
Using Barcode creator for Visual Studio .NET Control to generate, create GS1 128 image in .NET applications.
www.OnBarcode.com
1D Barcode Maker In Visual Basic .NET
Using Barcode maker for Visual Studio .NET Control to generate, create 1D Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Listing 2.8
Creating PDF 417 In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create PDF417 image in .NET framework applications.
www.OnBarcode.com
ANSI/AIM Code 39 Creation In None
Using Barcode maker for Microsoft Excel Control to generate, create Code39 image in Microsoft Excel applications.
www.OnBarcode.com
import import import import
Barcode Reader In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
www.OnBarcode.com
Making Barcode In Objective-C
Using Barcode creation for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Building a DOM with JAXP Imports the JAXP DOM classes
Barcode Generation In None
Using Barcode encoder for Online Control to generate, create Barcode image in Online applications.
www.OnBarcode.com
Create QR Code In VS .NET
Using Barcode creation for .NET framework Control to generate, create QR Code JIS X 0510 image in .NET applications.
www.OnBarcode.com
javax.xml.parsers.DocumentBuilderFactory; javax.xml.parsers.DocumentBuilder; org.w3c.dom.Document; java.io.File; {
public class JAXPandDOM
The Java APIs for XML
public static void main(String[] args) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder Obtains a DOMBuilder = factory.newDocumentBuilder(); via JAXP File ourExample = new File("product-catalog.xml"); Document document Parses the XML and = builder.parse( ourExample ); builds a DOM tree } catch (Exception e) { System.out.println(e.getMessage()); } } }
Using JAXP with XSLT JAXP supports XSLT in the same implementation-independent manner as XML parsing. The JAXP interfaces to XSLT are located in the javax.xml.transform package. The primary classes and interfaces are summarized in table 2.5. In addition to these top-level interfaces, JAXP includes three subpackages to support the use of SAX, DOM, and I/O streams with XSLT. These packages are summarized in table 2.6.
Table 2.5 Primary JAXP interfaces to the XSLT API
JAXP class or interface
javax.xml.transform.TransformerFactory
Description
Locates a TransformerFactory implementation class and instantiates it. Interface to the underlying XSLT processor. An interface representing an XML data source to be transformed by the Transformer. An interface to the output of the Transformer after XSLT processing.
javax.xml.transform.Transformer javax.xml.transform.Source
javax.xml.transform.Result
In section 2.1.3, we discussed the XSLT process and saw how our product catalog document could be transformed into HTML via XSLT. Now we examine how that XSLT process can be invoked from your Java code via JAXP. For the sake of clarity and simplicity, we will use the I/O stream helper classes
XML and Java
from the javax.xml.transform.stream package to create our Source and Result objects.
Table 2.6 JAXP helper packages for XSLT
Package name
javax.xml.transform.dom
Description
Contains classes and interfaces for using XSLT with DOM input sources and results. Contains classes and interfaces for using XSLT with SAX input sources and results. Contains classes and interfaces for using XSLT with I/O input and output stream sources and results.
javax.xml.transform.sax
javax.xml.transform.stream
The code we need to convert our example document to HTML is shown in listing 2.9. To compile it, you must have the JAXP jar file in your classpath. To run this program, you must have the example product catalog XML document from listing 2.1 saved in a file called product-catalog.xml. The stylesheet from listing 2.4 must be saved to a file named product-catalog-to-html.xsl. You can either type these files into your favorite editor or download them from the book s web site at http:/ /www.manning.com/gabrick. You will also need to place a JAXP-compliant XSLT engine (such as Xalan) in your classpath before testing this example.
Listing 2.9 Building a DOM with JAXP Imports the JAXP XSLT API
import javax.xml.transform.*; import javax.xml.transform.stream.*; import java.io.File; public class JAXPandXSLT {
public static void main(String[] args) { File sourceFile = new File("product-catalog.xml"); File xsltFile = new File("product-catalog-to-html.xsl"); Source xmlSource = new StreamSource(sourceFile); Source xsltSource = new StreamSource(xsltFile); Result result = new StreamResult(System.out); TransformerFactory factory = TransformerFactory.newInstance(); try {
Loads the XML and XSL files Creates I/O Stream sources and results
Returns an instance of
TransformerFactory
The Java APIs for XML
Transformer transformer = factory.newTransformer(xsltSource); transformer.transform(xmlSource, result);
B Factory returns new Transformer C
Performs transformation
} catch (TransformerConfigurationException tce) { System.out.println("No JAXP-compliant XSLT processor found."); } catch (TransformerException te) { System.out.println("Error while transforming document:"); te.printStackTrace(); } } }
The TransformerFactory implementation then provides its own specific Transformer implementation. Note that the transformation rules contained in the XSLT stylesheet are passed to the factory for it to create a Transformer object. This is the call that actually performs the XSLT transformation. Results are streamed to the specified Result stream, which is the console in this example.
At first glance, using XSLT via JAXP does not appear to be too complex. This is true for simple transformations, but there are many attributes of the XSLT process that can be configured via the Transformer and TransformerFactory interfaces. You can also create and register a custom error handler to deal with unexpected events during transformation. See the JAXP documentation for a complete listing of the possibilities. In this book, we concentrate on where and how you would use JAXP in your J2EE code rather than exhaustively exercising this API.
A word of caution Using XSLT, even via JAXP, is not without its challenges. The biggest barrier to the widespread use of XSLT is currently performance. Performing an XSLT transformation on an XML document is time- and resource-intensive. Some XSLT processors (including Xalan) allow you to precompile the transformation rules contained in your stylesheets to speed throughput. Through the JAXP 1.1 interface, it is not yet possible to access this feature. Proceed with caution and perform thorough load tests before using XSLT in production. If you need to use XSLT and if performance via JAXP is insufficient, you may consider using a vendor API directly and wrapping it in a utility component using the Fa ade pattern. You might also look into XSLTC, an XSLT compiler recently donated to the Apache Software Foundation by Sun
Copyright © OnBarcode.com . All rights reserved.