Parsing a Document
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
public class WellFormednessChecker {
public static void main(String[] args) {
Parser parser;
try {
parser = ParserFactory.makeParser();
}
catch (Exception e) {
// fall back on Xerces parser by name
try {
parser = ParserFactory.makeParser(
"org.apache.xerces.parsers.SAXParser");
}
catch (Exception eee) {
System.err.println("Couldn't locate a SAX parser");
return;
}
}
if (args.length == 0) {
System.out.println(
"Usage: java WellformednessChecker URL1 URL2...");
}
// start parsing...
for (int i = 0; i < args.length; i++) {
// command line should offer URIs or file names
try {
parser.parse(args[i]);
// If there are no well-formedness errors,
// then no exception is thrown
System.out.println(args[i] + " is well formed.");
}
catch (SAXException e) { // indicates a well-formedness error
System.out.println(args[i] + " is not well formed.");
System.out.println(e.getMessage());
}
catch (IOException e) {
System.out.println("Could not check " + args[i]
+ " because of the IOException " + e);
}
}
}
}