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 }
  • 相关阅读:
    JVM常用参数设置
    Jstat在分析java的内存GC时的应用
    jstack来分析linux服务器上Java应用服务性能异常
    linux 远程连接服务器ftp命令整理
    LR11中webservice协议的性能测试应用
    Windbg在.net性能问题排查hang情况的应用思路
    Windbg基本命令应用总结
    LR11直接对数据库访问操作方法在性能测试中的应用总结
    BenchmarkSQL v5.0测试达梦数据库
    SSH登录时间久,登录后报错:'abrt-cli status' timed out
  • 原文地址:https://www.cnblogs.com/sliencer/p/2416713.html
Copyright © 2011-2022 走看看