Using DOM for Syntax Checking : DOM « XML « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
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
Python Tutorial
Python Open Source
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
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » XML » DOMScreenshots 
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>joe@webserver.net</email>
       <name>Wang, Joe</name>
   </person>
   <person>
       <phone>704 555-0000</phone>
       <name>Pet, Rob</name>
       <email>rob@server.com</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. 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.