A DOM Error Checker: Using DOM for Syntax Checking : DOM Parser « XML « Java Tutorial






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) {
    boolean validate = true;

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(validate);
    dbf.setNamespaceAware(true);

    try {
      DocumentBuilder builder = dbf.newDocumentBuilder();
      builder.setErrorHandler(new MyErrorHandler());
      InputSource is = new InputSource("person.xml");
      Document doc = builder.parse(is);
    } catch (SAXException e) {
      System.out.println(e);
    } catch (ParserConfigurationException e) {
      System.err.println(e);
    } catch (IOException e) {
      System.err.println(e);
    }
  }
}

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());
  }
}
//File: person.xml
<?xml version="1.0" standalone="yes"?>

<folks>
    <person>
        <phone>999 555-8888</phone>
        <email>e@yoursite.net</email>
        <name>R, L</name>
    </person>
    <person>
        <phone>333 555-9910</phone>
        <name>V, R</name>
        <email>rv@yoursite.com</email>
    </person>
    <person>
        <name>B, D.</name>
        <email>b@xyz.net</email>
    </person>
    <person>
        <email>c@n.net</email>
        <name>C, X</name>
    </person>
    <person>
        <phone>502 555-2192</phone>
        <name>B, M</name>
    </person>
</folks>
Error: Document is invalid: no grammar found.
Line 3 Column 7
System ID: file:///C:/Java_Dev/eclipse31/Eclipse/person.xml
org.xml.sax.SAXParseException: Document is invalid: no grammar found.








33.2.DOM Parser
33.2.1.DOM Objects That Make Up the Parse Tree
33.2.2.A DOM Error Checker: Using DOM for Syntax Checking
33.2.3.A DOM Parse Tree Lister
33.2.4.Listing the Contents of Parse Tree Nodes: Using the DOM Parser to Extract XML Document Data
33.2.5.Ignorable Whitespace and Element Content
33.2.6.Remove the element from parent
33.2.7.Visiting All the Elements in a DOM Document
33.2.8.Getting the Root Element in a DOM Document
33.2.9.Getting a Node Relative to Another Node in a DOM Document
33.2.10.Getting the Notations in a DOM Document
33.2.11.Getting the Declared Entities in a DOM Document
33.2.12.Getting the Value of an Entity Reference in a DOM Document
33.2.13.Getting a DOM Element by Id
33.2.14.Converting an XML Fragment into a DOM Fragment
33.2.15.Parse an XML string: Using DOM and a StringReader.
33.2.16.Use DOM L3 DOMBuilder, DOMBuilderFilter DOMWriter and other DOM L3 functionality to preparse, revalidate and safe document.
33.2.17.Read XML as DOM
33.2.18.Create DOM Document out of string
33.2.19.Source To InputSource