zoukankan      html  css  js  c++  java
  • Java : use xsd 校验 xml

     1 import javax.xml.parsers.DocumentBuilder;
     2 import javax.xml.parsers.DocumentBuilderFactory;
     3 
     4 import org.w3c.dom.Document;
     5 import org.xml.sax.ErrorHandler;
     6 import org.xml.sax.InputSource;
     7 import org.xml.sax.SAXException;
     8 import org.xml.sax.SAXParseException;
     9 
    10 import java.io.*;
    11 import javax.xml.transform.Source;
    12 import javax.xml.transform.stream.StreamSource;
    13 import javax.xml.validation.*;
    14 import org.xml.sax.SAXException;
    15 
    16 public class XValidator {
    17 
    18     /**
    19      * @param args
    20      */
    21     public static void main(String[] args) throws SAXException, IOException {
    22 
    23         System.out.println("Validation file: "+ args[1]);
    24         System.out.println("with: "+ args[0]);
    25         System.out.println();
    26         // 1. Lookup a factory for the W3C XML Schema language
    27         SchemaFactory factory = 
    28             SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    29         
    30         // 2. Compile the schema. 
    31         // Here the schema is loaded from a java.io.File, but you could use 
    32         // a java.net.URL or a javax.xml.transform.Source instead.
    33         File schemaLocation = new File(args[0]);
    34         Schema schema = factory.newSchema(schemaLocation);
    35     
    36         // 3. Get a validator from the schema.
    37         Validator validator = schema.newValidator();
    38         
    39         // 4. Parse the document you want to check.
    40         Source source = new StreamSource(args[1]);
    41         
    42         // 5. Check the document
    43         try {
    44             validator.validate(source);
    45             System.out.println(args[0] + " is valid.");
    46         }
    47         catch (SAXException ex) {
    48             System.out.println("Error: " + args[0] + " is not valid because: ");
    49             System.out.println(ex.getMessage());
    50         }  
    51         
    52     }
    53 
    54 }
    55 class SimpleErrorHandler implements ErrorHandler{
    56 
    57     @Override
    58     public void error(SAXParseException arg0) throws SAXException {
    59         System.out.printf("error=%s\n", arg0.getMessage());
    60     }
    61 
    62     @Override
    63     public void fatalError(SAXParseException arg0) throws SAXException {
    64         System.out.printf("fatalError=%s\n", arg0.getMessage());
    65         
    66     }
    67 
    68     @Override
    69     public void warning(SAXParseException arg0) throws SAXException {
    70         System.out.printf("warning=%s\n", arg0.getMessage());
    71         
    72     }
    73     
    74 }
  • 相关阅读:
    java面试题之简单介绍一下集合框架
    java面试题之hashcode相等两个类一定相等吗?equals呢?相反呢?
    java面试题之什么是ThreadLocal?底层如何实现的?
    java面试题之stop()和suspend()方法为何不不推荐使⽤?
    设计模式—单例模式
    Java并发—同步容器和并发容器
    Java并发—并发工具类
    Java并发—原子类,java.util.concurrent.atomic包(转载)
    Java并发—java.util.concurrent.locks包
    Java并发—java.util.concurrent并发包概括(转载)
  • 原文地址:https://www.cnblogs.com/sliencer/p/2416713.html
Copyright © 2011-2022 走看看