java xsd 校验工具类

    xiaoxiao2021-03-25  82

    public static boolean Validatexml(String xsdpath,String xmlpath) throws SAXException,IOException{ //建立schema工厂 SchemaFactory schemaFactory=SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); //建立验证文档文件对象,利用此文件对象所封装的文件进行schema验证 File schemaFile=new File(xsdpath); //利用schema工厂,接收验证文档文件对象生成Schema对象 Schema schema=schemaFactory.newSchema(schemaFile); //通过Schema产生针对于此Schema的验证器,利用schenaFile进行验证 Validator validator=schema.newValidator(); //得到验证的数据源 Source source=new StreamSource(xmlpath); //开始验证,成功输出success!!!,失败输出fail try{ validator.validate(source); }catch(Exception ex){ ex.printStackTrace(); return false; } return true; }

    所需文件 http://download.csdn.net/my

    测试类

    boolean judge = Validatexml(xsdPath,xmlPath); System.out.println(judge); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
    转载请注明原文地址: https://ju.6miu.com/read-19568.html

    最新回复(0)