当前位置: 首页 > 工具软件 > XSD/e > 使用案例 >

java xsd_java 校验XSD文件

松新
2023-12-01

packageebshk.adsr.validate;importorg.apache.log4j.Logger;importjavax.xml.XMLConstants;importjavax.xml.transform.Source;importjavax.xml.transform.stream.StreamSource;importjavax.xml.validation.Schema;importjavax.xml.validation.SchemaFactory;importjavax.xml.validation.Validator;importjava.io.StringReader;public classValidateUtil {private static Logger logger = Logger.getLogger(ValidateUtil.class);public staticBoolean validate(String xml){

Boolean pass= true;try{

Source root= new StreamSource(ValidateUtil.class.getClassLoader()

.getResourceAsStream("xsd/root_elements.xsd"));

Source code= new StreamSource(ValidateUtil.class.getClassLoader()

.getResourceAsStream("xsd/code_lists.xsd"));

Source simple= new StreamSource(ValidateUtil.class.getClassLoader()

.getResourceAsStream("xsd/simple_types.xsd"));

Source ccd= new StreamSource(ValidateUtil.class.getClassLoader()

.getResourceAsStream("xsd/client_centric_data.xsd"));

Source customer= new StreamSource(ValidateUtil.class.getClassLoader()

.getResourceAsStream("xsd/customer_account.xsd"));

Source sub= new StreamSource(ValidateUtil.class.getClassLoader()

.getResourceAsStream("xsd/sub_account.xsd"));

Source trade= new StreamSource(ValidateUtil.class.getClassLoader()

.getResourceAsStream("xsd/trading_account.xsd"));

Source[] sourceArr={code, simple, ccd, customer, sub, trade, root};

Schema schema=SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(sourceArr);

Validator validator=schema.newValidator();

Source s= new StreamSource(newStringReader(xml));

validator.validate(s);

}catch(Exception e){

e.printStackTrace();

logger.info(e.getMessage());

pass= false;

}returnpass;

}

}

 类似资料: