zoukankan      html  css  js  c++  java
  • JBPM流程部署校验之java利用XSD校验XML

    JBPM流程定义校验之java利用XSD校验XML

             上篇我们学习了在.net中怎样利用XSD来验证xml,今天我们来看一下在java中怎样实现利用xsd来校验xml!

            

    /**
     * 
     
    */
    package WFTH;

    import java.io.*;
    import javax.xml.transform.Source;
    import javax.xml.transform.stream.StreamSource;
    import javax.xml.validation.*;
    import org.xml.sax.SAXException;

    /**
     * 
    @author 无风听海
     *
     
    */
    public class ValidationManager {
         
    public static String ValidationXmlByXSD(String filePath,String xsdPath,String nameSpace)throws SAXException, IOException {         
             String msg
    =filePath + " is valid.";
             SchemaFactory factory 
    = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
             Schema schema
    =null;
             
    //xml和xsd是独立的文件,否则是内联文件
             if(xsdPath!=null&&xsdPath!=""){
                 File schemaLocation 
    = new File(xsdPath);
                 schema 
    = factory.newSchema(schemaLocation);  
             }
             
    else{
                 schema 
    = factory.newSchema();  
             }        
             Validator validator 
    = schema.newValidator();       
             Source source 
    = new StreamSource(filePath);    
             
    try {
                 validator.validate(source);            
             }
             
    catch (SAXException ex) {
                 msg
    = filePath + " is not valid because "+  ex.getMessage();
             }      
             
    return msg;
         } 
    }


     

  • 相关阅读:
    面向对象编程思想-单例模式
    Bootstrap(四):CSS--表格
    Bootstrap(三):CSS--代码
    Git 学习第三天(一)
    Git 学习第四天
    Git 学习第二天(三)
    Git 学习第三天(二)
    Git 学习第二天(一)
    GIT 学习第二天 (二)
    Git 学习第一天
  • 原文地址:https://www.cnblogs.com/wufengtinghai/p/2137672.html
Copyright © 2011-2022 走看看