Chapter 4 XML Processing
177
chain of filters and each filter implements a processing step. XML processing
techniques such as SAX work well with this design.
The two designs can, of course, be combined. When combined, transforma
tions and identity transformations become handy techniques to use when the result
of a processing step cannot be directly wrapped into a
Source
object compatible
with the next step. JAXP also provides support for chaining transformations with
the use of
javax.xml.transform.sax.SAXTransformerFactory
.
Code Example 4.10 illustrates an XML processing pipeline that combines
SAX and XSLT to validate an incoming purchase order document, extract on the
fly the purchase order identifier, and transform the incoming document from its
external, XSD based schema to the internal, DTD based schema supported by the
business logic. The code uses a SAX filter chain as the
Source
of a transforma
tion. Alternatively, the code could have used a
SAXTransformerFactory
to create
an
org.xml.sax.XMLFilter
to handle the transformation and then chain it to the
custom
XMLFilter
, which extracts the purchase order identifier.
public class SupplierOrderXDE extends
XMLDocumentEditor.DefaultXDE {
public static final String DEFAULT_ENCODING = "UTF 8";
private XMLFilter filter;
private Transformer transformer;
private Source source = null;
private String orderId = null;
public SupplierOrderXDE(boolean validating, ...) {
// Create a [validating] SAX parser
SAXParser parser = ...;
filter = new XMLFilterImpl(parser.getXMLReader()) {
// Implements a SAX XMLFilter that extracts the OrderID
// element value and assigns it to the orderId attribute
};
// Retrieve the style sheet as a stream
InputStream stream = ...;
// Create a transformer from the stylesheet
transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource(stream));
}
// Sets the document to be processed
New Page 1