Using DOM for Syntax Checking : DOM : XML : Java examples (example source code) Organized by topic

Java
C++
Java Home »  XML   » [  DOM  ]  Screenshots 
 



Using DOM for Syntax Checking

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

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

<folks>
   <person>
       <phone>306 555-9999</phone>
       <email>[email protected]</email>
       <name>Yin, Joe</name>
   </person>
   <person>
       <phone>704 555-0000</phone>
       <name>Pet, Rob</name>
       <email>[email protected]</email>
   </person>
</folks>

*/

import java.io.IOException;

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) {
           if(!arg[0].equals("-v"))
               usage();
           validate = true;
           filename = arg[1];
       else {
           usage();
       }

       // 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 = 
DocumentBuilderFactory.newInstance();
       dbf.setValidating(validate);
       dbf.setNamespaceAware(true);

       // 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) {
           System.exit(1);
       catch (ParserConfigurationException e) {
           System.err.println(e);
           System.exit(1);
       catch (IOException e) {
           System.err.println(e);
           System.exit(1);
       }
   }
   private static void usage() {
       System.err.println("Usage: DOMCheck [-v] <filename>");
       System.exit(1);
   }
}
class MyErrorHandler implements ErrorHandler {
  public void warning(SAXParseException ethrows SAXException {
    show("Warning", e);
    throw (e);
  }

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

  public void fatalError(SAXParseException ethrows 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.  XML Document information by DOM
2.  Using the DOM Parser to Build a Document Tree Using the DOM Parser to Build a Document Tree
3.  DOM Features DOM Features
4.  DOM level 2 Events DOM level 2 Events
5.  Searching through a document
6.  Check a vendor's DOM implementation Check a vendor's DOM implementation
7.  List an XML file after building it into a JDOM Document List an XML file after building it into a JDOM Document
8.  Make up and write an XML document, using JDOM Make up and write an XML document, using JDOM
9.  Make up and write an XML document, using DOM Make up and write an XML document, using DOM
10.  Simple demo of JDOM








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.