Example The DOM DOMErrorHandler interface

public boolean handleError DOMError error In Xerces-2, the XMLSerializer class implements the DOMWriter interface, so if you prefer you can use these methods instead of the ones discussed in the last section. Example 13.5 demonstrates a complete program that builds a simple SVG document in memory and writes it into the file circle.svg in the current working directory using a r n line end and the UTF-16 encoding. The error handler is set to an anonymous inner class that prints error messages on...

DTDHandler

SAX is mostly about the instance document, not the DTD or schema. However, given a validating parser, or at least an internal DTD subset, the DTD can affect the contents of the instance document in six ways 1. It can provide default values for attributes. 2. It can assign types to attributes which affects their normalized value. 3. It can distinguish between ignorable and non-ignorable white space. 4. It can declare general entities. 5. It can declare unparsed entities. The first four are...

Example The TrAX OutputKeys class

package javax.xml.transform public class OutputKeys private OutputKeys public static final String METHOD method public static final String VERSION version public static final String ENCODING encoding public static final String OMIT_XML_DECLARATION omit-xml-declaration public static final String STANDALONE standalone public static final String DOCTYPE_PUBLIC doctype-public public static final String DOCTYPE_SYSTEM doctype-system public static final String CDATA_SECTION_ELEMENTS...

The Last Mile

Schematron is powerful, but there are still some checks it cannot perform. In particular, it cannot perform any checks that require information external to the document and the schema. For example, it cannot verify that the page at a referenced URL is reachable. It cannot verify that a file exists on the local file system. It cannot compare the SKUs, names, and prices in an order document with their values in a remote database. None of the extant schema languages allow you to state conditions...

Example The TrAX StreamResult class

public class StreamResult implements Result public static final String FEATURE public StreamResult OutputStream outputStream public StreamResult Writer writer public StreamResult String systemID public StreamResult File f public void setOutputStream OutputStream outputStream public OutputStream getOutputStream public void setWriter Writer writer public void setSystemId String systemID public void setSystemId File f You should specify the system ID URL and one of the other identifiers File,...

Example The SAXException class

public class SAXException extends Exception public SAXException Exception rootCause public SAXException String message, Exception e public String public Exception public String SAXException may not always be the exception you want to throw, however. For example, suppose you're parsing a document containing an XML digital signature, and the endElement method notices that the Base-64 encoded text provided in the P element, which represents the prime modulus of a DSA key, does not decode to a...

Example A URIResolver class

public class GZipURIResolver implements URIResolver public Source resolve String href, String base try GZIPInputStream gin new GZIPInputStream in return new StreamSource gin, u.toString If anything goes wrong, just return null and let the default resolver try. The following two methods in TransformerFactory set and get the URIResolver that Transformer objects created by this factory will use to resolve URIs public abstract void setURIResolver URIResolver resolver public abstract URIResolver...

Example The JDOM Comment class

public class Comment implements Serializable, Cloneable protected String text protected Object parent protected Comment public Comment String text public Element getParent protected Comment setParent Element parent public Comment detach public Document getDocument protected Comment setDocument Document document public Comment setText String text public final boolean equals Object ob public final int hashCode For example, this code fragment adds the comment lt --An example from Chapter 15 of...

Example The TrAX SAXSource class

public class SAXSource implements Source public static final String FEATURE public SAXSource XMLReader reader, InputSource inputSource public SAXSource InputSource inputSource public void setXMLReader XMLReader reader public void setInputSource InputSource inputSource public InputSource getInputSource public void setSystemId String systemID public static InputSource sourceToInputSource Source source The SAXResult class, shown in Example 17.16, receives output from the XSLT processor as a stream...

Example Navigating a subtree with TreeWalker

import javax.xml.parsers. import org.w3c.dom. import org.w3c.dom.traversal. import org.xml.sax.SAXException import java.io.IOException public static void printExampleTitles Document doc Create the NodeIterator DocumentTraversal traversable DocumentTraversal doc TreeWalker walker traversable.createTreeWalker doc.getDocumentElement , NodeFilter.SHOW_ELEMENT, new ExampleFilter , true The TreeWalker starts out positioned at the root Node chapter walker.firstChild int chapterNumber 0 while chapter...

Example The DocumentTraversal factory interface

public interface DocumentTraversal public NodeIterator createNodeIterator Node root, int whatToShow, NodeFilter filter, boolean entityReferenceExpansion throws DOMException public TreeWalker createTreeWalker Node root, int whatToShow, NodeFilter filter, boolean entityReferenceExpansion throws DOMException Thus, to create a NodeIterator you cast the Document object you want to iterate over to DocumentTraversal, and then invoke its createNodeIterator method. This method takes four arguments The...