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

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » XML » DOM Parser 
33. 2. 2. A DOM Error Checker: Using DOM for Syntax Checking
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 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());
  }
}
//File: person.xml
<?xml version="1.0" standalone="yes"?>

<folks>
    <person>
        <phone>999 555-8888</phone>
        <email>[email protected]</email>
        <name>R, L</name>
    </person>
    <person>
        <phone>333 555-9910</phone>
        <name>V, R</name>
        <email>[email protected]</email>
    </person>
    <person>
        <name>B, D.</name>
        <email>[email protected]</email>
    </person>
    <person>
        <email>[email protected]</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
w__w___w.___j__a__va__2_s_.__c_o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.