Professional Documents
Culture Documents
The Simple Access for XML (SAX) API is based on an event-driven processing model where the data
elements are interpreted on a sequential basis and callbacks are called based on selected constructs.
SAX’s biggest advantage is that it does not load any XML documents into memory; therefore it is
considered to be very fast and lightweight.
1. Implement a class that extends the DefaultHandler and holds callback methods for every type
of construct found in XML that contains implementation based on your needs.
2. Instantiate a new SAX Parser class. The Parser reads the XML source file and triggers a callback
implemented in the DefaultHandlerclass.
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
javax.xml.parsers.SAXParser saxParser = saxFactory.newSAXParser();
3. Read the XML source sequentially: Once the parser is obtained, is to call the parse() method by
passing the XML data to be parsed and a Handler class to handle the data that is being passed.
1. Obtain a new instance of the DOM Factory class by calling the DocumentBuilderFactory
class’s newInstance() static method:
DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
2. Instantiate a new document builder class, once the factory is obtained, by calling the
newDocumentBuilder() static method:
DocumentBuilder builder = factory.newDocumentBuilder();
The document builder is used for loading the XML data into memory and for creating the
Document object representation of the XML data.
3. Parse, using the Builder class, which contains a parse method that accepts an input stream
representing the XML data:
Document document = builder.parse(PurchaseOrder.xml);
Transforming with XSLT
Extensible Style sheet Language Transformation (XSLT) is the process by which XSL templates are applied
to XML documents in order to create new documents in desired formats. XSL provides the syntax and
semantics for specifying formatting, and XSLT is the processor that performs the formatting task.
XSL Syntax
Many operations can be used to process the data in XML, including locating a particular element,
iterating through a document, the conditional processing of elements, sorting, and numbering.
Templates used for locating parts of a document
Conditional processing, which is done only when certain criteria are examined
Looping used for iterating through results
Sorting used for displaying the output in some logical order
The following is a snippet of a template rule definition in an XSL stylesheet file:
<xsl:value-of> tag puts the value of the selected node as per XPath expression,
as text.
2. Use the transformer class for applying the stylesheet to the input XML data. When getting a new instance,
the stylesheet is passed as a parameter to the newTransformer() method:
Transformer transformer = factory.newTransformer(new
StreamSource(“order.xsl”));
3. The transformer then calls the transform method to invoke the transformation process. The parameters
required in the transform method are input stream and output result: