⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ddvalidator.java

📁 JAVA Servlet2.3外文书籍源码
💻 JAVA
字号:
package deployment;
import java.io.*;
// JAXP packages
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.w3c.dom.*;

public class DDValidator {
  private String xmlfile;

  public DDValidator(String xmlfile) {
    this.xmlfile = xmlfile;
  }

  private boolean isValid() throws SAXException {

    // Step 1: create a DocumentBuilderFactory and configure it
    DocumentBuilderFactory dbf =
    DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);

    // Step 2: create a DocumentBuilder that satisfies the constraints
    // specified by the DocumentBuilderFactory
    DocumentBuilder db = null;
    try {
      db = dbf.newDocumentBuilder();

      //  Inner class to handle errors in parsing the document
      db.setErrorHandler(new DefaultHandler() {
         public void error(SAXParseException e) throws SAXException {
            throw new SAXException ("ERROR : " + e.getMessage());
         }
         public void fatalError(SAXParseException e) throws SAXException {
            throw new SAXException ("FATAL ERROR : " + e.getMessage());
         }

      });
    } catch (ParserConfigurationException pce) {
      System.err.println("Exception with configuration" + pce);
      System.exit(1);
    }

    //  Step 3: create a dom by parsing the document.
    //  any parse errors will cause an exception to be thrown
    Document doc = null;
    try {

      doc = db.parse(new File(this.xmlfile));

    } catch (IOException ioe) {
      // If the file was not valid, throw an exception
      throw new SAXException("Document Invalid " + ioe.getMessage());
    }

    // This point is only ever reached if the XML was validated
    return true;

  }

  public static void main(String args[]) {
    //  Get the name of the XML doc
    if (args.length != 1) {
      System.err.println("Usage: DDValidator xml_file_to_validate");
      System.exit(0);
    }

    String xmlfile = args[0];
    DDValidator validator = new DDValidator(xmlfile);

    try {
      System.out.println(validator.isValid());
    } catch (SAXException se) {
      System.err.println(se.getMessage());
    }
  }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -