Professional Documents
Culture Documents
Part II
Ethan Cerami
New York University
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import java.io.IOException;
/**
* SAX Validator.
* Illustrates Basic Error Handling.
*/
10/17/08 Simple API for XML (SAX), Part II 12
public class SAXValidator extends DefaultHandler {
private boolean isValid = true;
/**
* Receives notification of a recoverable error.
* Validation Errors are reported here.
*/
public void error(SAXParseException exception)
throws SAXException {
isValid = false;
reportError("Error", exception);
}
Log Errors and
/** Warnings
* Receives notification of a warning.
*/
public void warning(SAXParseException exception)
throws SAXException {
reportError("Warning", exception);
}
/**
* Prints Command Line Usage
*/
private static void printUsage() {
System.out.println("usage: SAXValidator xml-file");
System.exit(0);
}
<table>
<name>Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
All qualified names that begin with “h” are within the
XHTML namespace.
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;
import org.xml.sax.XMLReader;
import org.xml.sax.Locator;
import java.io.IOException;
/**
* SAXElementAttribute.
* Illustrates Elements, Attributes and Namespace Functionality.
* Also illustrates use of Document Locator object.
*/
public class SAXElementAttribute extends DefaultHandler {
private Locator _locator;
/**
* End Prefix Mapping for XML Namespaces
*/
public void endPrefixMapping(String prefix) throws SAXException {
System.out.println ("End Prefix Mapping: "+prefix);
}
/**
* Stores Document Locator
*/
public void setDocumentLocator (Locator locator) {
this._locator = locator;
}
10/17/08 Simple API for XML (SAX), Part II 33
/**
* Prints Command Line Usage
*/
private static void printUsage() {
System.out.println ("usage: SAXElementAttribute xml-file");
System.exit(0);
}
/**
* Main Method
*/
public static void main(String[] args) {
if (args.length != 1) {
printUsage();
}
try {
SAXElementAttribute saxHandler = new SAXElementAttribute();
XMLReader parser = XMLReaderFactory.createXMLReader
("org.apache.xerces.parsers.SAXParser");