Use DOM L3 DOMBuilder, DOMBuilderFilter DOMWriter and other DOM L3 functionality to preparse, revalidate and safe document. : 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
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 Tutorial » XML » DOM Parser 
33. 2. 16. Use DOM L3 DOMBuilder, DOMBuilderFilter DOMWriter and other DOM L3 functionality to preparse, revalidate and safe document.
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 
 *      http://www.apache.org/licenses/LICENSE-2.0
 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMError;
import org.w3c.dom.DOMErrorHandler;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSParser;
import org.w3c.dom.ls.LSParserFilter;
import org.w3c.dom.ls.LSSerializer;
import org.w3c.dom.traversal.NodeFilter;

/**
 * This sample program illustrates how to use DOM L3 DOMBuilder,
 * DOMBuilderFilter DOMWriter and other DOM L3 functionality to preparse,
 * revalidate and safe document.
 */
public class DOM3 implements DOMErrorHandler, LSParserFilter {

  /** Default namespaces support (true). */
  protected static final boolean DEFAULT_NAMESPACES = true;

  /** Default validation support (false). */
  protected static final boolean DEFAULT_VALIDATION = false;

  /** Default Schema validation support (false). */
  protected static final boolean DEFAULT_SCHEMA_VALIDATION = false;

  static LSParser builder;

  public static void main(String[] argv) {

    if (argv.length == 0) {
      printUsage();
      System.exit(1);
    }

    try {

      // get DOM Implementation using DOM Registry
      System.setProperty(DOMImplementationRegistry.PROPERTY,
          "org.apache.xerces.dom.DOMXSImplementationSourceImpl");
      DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

      DOMImplementationLS impl = (DOMImplementationLSregistry.getDOMImplementation("LS");

      // create DOMBuilder
      builder = impl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

      DOMConfiguration config = builder.getDomConfig();

      // create Error Handler
      DOMErrorHandler errorHandler = new DOM3();

      // create filter
      LSParserFilter filter = new DOM3();

      builder.setFilter(filter);

      // set error handler
      config.setParameter("error-handler", errorHandler);

      // set validation feature
      // config.setParameter("validate", Boolean.FALSE);
      config.setParameter("validate", Boolean.TRUE);

      // set schema language
      config.setParameter("schema-type""http://www.w3.org/2001/XMLSchema");
      // config.setParameter("psvi",Boolean.TRUE);
      // config.setParameter("schema-type","http://www.w3.org/TR/REC-xml");

      // set schema location
      config.setParameter("schema-location""personal.xsd");

      // parse document
      System.out.println("Parsing " + argv[0"...");
      Document doc = builder.parseURI(argv[0]);

      // set error handler on the Document
      config = doc.getDomConfig();

      config.setParameter("error-handler", errorHandler);

      // set validation feature
      config.setParameter("validate", Boolean.TRUE);
      config.setParameter("schema-type""http://www.w3.org/2001/XMLSchema");
      // config.setParameter("schema-type","http://www.w3.org/TR/REC-xml");
      config.setParameter("schema-location""data/personal.xsd");

      // remove comments from the document
      config.setParameter("comments", Boolean.FALSE);

      System.out.println("Normalizing document... ");
      doc.normalizeDocument();

      // create DOMWriter
      LSSerializer domWriter = impl.createLSSerializer();

      System.out.println("Serializing document... ");
      config = domWriter.getDomConfig();
      config.setParameter("xml-declaration", Boolean.FALSE);
      // config.setParameter("validate",errorHandler);

      // serialize document to standard output
      // domWriter.writeNode(System.out, doc);
      LSOutput dOut = impl.createLSOutput();
      dOut.setByteStream(System.out);
      domWriter.write(doc, dOut);

    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private static void printUsage() {

    System.err.println("usage: java dom.DOM3 uri ...");
    System.err.println();
    System.err.println("NOTE: You can only validate DOM tree against XML Schemas.");

  // printUsage()

  public boolean handleError(DOMError error) {
    short severity = error.getSeverity();
    if (severity == DOMError.SEVERITY_ERROR) {
      System.out.println("[dom3-error]: " + error.getMessage());
    }

    if (severity == DOMError.SEVERITY_WARNING) {
      System.out.println("[dom3-warning]: " + error.getMessage());
    }
    return true;

  }

  /**
   @see org.w3c.dom.ls.LSParserFilter#acceptNode(Node)
   */
  public short acceptNode(Node enode) {
    return NodeFilter.FILTER_ACCEPT;
  }

  /**
   @see org.w3c.dom.ls.LSParserFilter#getWhatToShow()
   */
  public int getWhatToShow() {
    return NodeFilter.SHOW_ELEMENT;
  }

  /**
   @see org.w3c.dom.ls.LSParserFilter#startElement(Element)
   */
  public short startElement(Element elt) {
    return LSParserFilter.FILTER_ACCEPT;
  }

}
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.