FileDocCategorySizeDatePackage
XParse.javaAPI DocExample2196Sat Mar 13 17:21:04 GMT 2004None

XParse

public class XParse extends Object
Parse an XML file using DOM, via JAXP.
author
Ian Darwin, http://www.darwinsys.com/
version
$Id: XParse.java,v 1.9 2004/03/13 23:21:04 ian Exp $

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] av)

		if (av.length == 0) {
			System.err.println("Usage: XParse file");
			return;
		}
		boolean validate = false;
		for (int i=0; i<av.length; i++) {
			if (av[i].equals("-v"))
				validate = true;
			else
				parse(av[i], validate);
		}
	
public static voidparse(java.lang.String fileName, boolean validate)
Parse the file

		try {
			System.err.println("Parsing " + fileName + "...");

			// Make the document a URL so relative DTD works.
			String uri = "file:" + new File(fileName).getAbsolutePath();

			DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
			if (validate)
				f.setValidating(true);
			DocumentBuilder p = f.newDocumentBuilder();
			// Get local copies of DTDs...
			p.setEntityResolver(new MyDTDResolver());
			Document doc = p.parse(uri);
			System.out.println("Parsed OK");

		} catch (SAXParseException ex) {
			System.err.println("+================================+");
			System.err.println("|       *SAX Parse Error*        |");
			System.err.println("+================================+");
			System.err.println(ex.toString());
			System.err.println("At line " + ex.getLineNumber());
			System.err.println("+================================+");
		} catch (SAXException ex) {
			System.err.println("+================================+");
			System.err.println("|          *SAX Error*           |");
			System.err.println("+================================+");
			System.err.println(ex.toString());
			System.err.println("+================================+");
		} catch (Exception ex) {
			System.err.println("+================================+");
			System.err.println("|           *XML Error*          |");
			System.err.println("+================================+");
			System.err.println(ex.toString()); 
		}