Using DOM for Syntax Checking : DOM « XML « Java

Using DOM for Syntax Checking

//Example XML document
An XML Document Containing a Simple Contact List
Start example

<?xml version="1.0" standalone="yes"?>

       <phone>306 555-9999</phone>
       <name>Wang, Joe</name>
       <phone>704 555-0000</phone>
       <name>Pet, Rob</name>



import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class DOMCheck {
   static public void main(String[] arg) {
       String filename = null;
       boolean validate = false;

       if(arg.length == 1) {
           filename = arg[0];
       } else if(arg.length == 2) {
           validate = true;
           filename = arg[1];
       } else {

       // Create a new factory to create parsers that will
       // be aware of namespaces and will validate or
       // not according to the flag setting.
       DocumentBuilderFactory dbf = 

       // Use the factory to create a parser (builder) and use
       // it to parse the document.
       try {
           DocumentBuilder builder = dbf.newDocumentBuilder();
           builder.setErrorHandler(new MyErrorHandler());
           InputSource is = new InputSource(filename);
           Document doc = builder.parse(is);
       } catch (SAXException e) {
       } catch (ParserConfigurationException e) {
       } catch (IOException e) {
   private static void usage() {
       System.err.println("Usage: DOMCheck [-v] <filename>");
class MyErrorHandler implements ErrorHandler {
  public void warning(SAXParseException e) throws SAXException {
    show("Warning", e);
    throw (e);

  public void error(SAXParseException e) throws SAXException {
    show("Error", e);
    throw (e);

  public void fatalError(SAXParseException e) throws SAXException {
    show("Fatal Error", e);
    throw (e);

  private void show(String type, SAXParseException e) {
    System.out.println(type + ": " + e.getMessage());
    System.out.println("Line " + e.getLineNumber() + " Column "
        + e.getColumnNumber());
    System.out.println("System ID: " + e.getSystemId());


Related examples in the same category

1.Parsing a Document Using JAXP
2.XML Document information by DOM
3.Using the DOM Parser to Build a Document TreeUsing the DOM Parser to Build a Document Tree
4.DOM FeaturesDOM Features
5.DOM level 2 EventsDOM level 2 Events
6.Searching through a document
7.Check a vendor's DOM implementationCheck a vendor's DOM implementation
8.Make up and write an XML document, using DOMMake up and write an XML document, using DOM
9.Creating XML Document using DOM
10.Loading an XML Document using DOM
11.Parse an XML string: Using DOM and a StringReader.
12.Create an XML document with DOM
13.Extracting an XML formatted string out of a DOM object
14.Reading an XML Document and create user-defined object from DOM
15.Visiting All the Nodes in a DOM Document
16.Generating SAX Parsing Events by Traversing a DOM Document
17.Converting an XML Fragment into a DOM Fragment
18.A utility class which provides methods for working with a W3C DOM
19.XML DOM Utilities
20.Convenience methods for working with the DOM API
21.DOM Utils
22.Utilities to read DOM
23.W3C DOM utility methods
24.Read XML as DOM
25.Utility method for parsing the XML with DOM
26.Handles DOM processing allowing the reading and writing of hierarchical structures as XML files.
27.Xml Utils for dom4j
28.DocWriter has a static method for writing XML documents with a writer