XSD与XML的检测示例代码

xml与xsd的匹配性检测示例

import java.io.File;

import javax.xml.transform.source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;

public class XmlHandler {

	public static final String XSD_FILE_PATH = "automation.xsd";
    public static void checkXSD(File xmlFile)  {
     
    	SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    	try {
    		schemaFactory.newSchema(
                    new Source[] {
                   		 new StreamSource(
                   				 Thread.currentThread().getContextClassLoader().getResourceAsstream(XSD_FILE_PATH))}
                    ).newValidator().validate(new StreamSource(xmlFile));
		} catch (Exception e) {
			throw new RuntimeException("XSD检测失败",e);
		}
			
    }
}

不抛异常表示检测通过。

以上就是XSD与XML的检测示例代码的详细内容,更多请关注编程之家其它相关文章

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念