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 }
  • 相关阅读:
    xls与csv文件的区别
    青音,经典爱情语录
    win7用户账户自动登录方法汇总
    How to using Procedure found Lead Blocker
    FTS(3) BSD 库函数手册 遍历文件夹(二)
    FTS(3) BSD 库函数手册 遍历文件夹(一)
    DisplayMetrics类 获取手机显示屏的基本信息 包括尺寸、密度、字体缩放等信息
    About App Distribution 关于应用发布
    FTS(3) 遍历文件夹实例
    OpenCV 2.1.0 with Visual Studio 2008
  • 原文地址:https://www.cnblogs.com/sliencer/p/2416713.html
Copyright © 2011-2022 走看看