JAXB: 通过schema验证XML文件

在使用JAXB解析XML文件的时候我们有时候可能会需要通过schema文件验证XML的格式,接上文的XMLParser.java

 

示例代码如下:

Java代码   收藏代码
  1. public   static  Object unmarshal(InputStream xml, Class<?> clazz)  throws  SAXException {  
  2.         Object obj = null ;  
  3.           
  4.         ValidationEventCollector vec = new  ValidationEventCollector();  
  5.           
  6.         try  {  
  7.             SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);  
  8.             Schema schema = sf.newSchema(new  File( "C://eclipse//workspace1//STAF//test//employees.xsd" ));  
  9.               
  10.             JAXBContext jc = JAXBContext.newInstance(clazz.getPackage().getName());  
  11.             Unmarshaller u = jc.createUnmarshaller();  
  12.             u.setSchema(schema);  
  13.             u.setEventHandler(vec);  
  14.               
  15.             obj = u.unmarshal(xml);  
  16.         } catch  (JAXBException e) {  
  17.             throw   new  RuntimeException( "Can't unmarshal the XML file, error message: "  + e.getMessage());  
  18.         } finally  {  
  19.             if (vec !=  null  && vec.hasEvents()) {  
  20.                 for (ValidationEvent ve : vec.getEvents()) {  
  21.                     String msg = ve.getMessage();  
  22.                     ValidationEventLocator vel = ve.getLocator();  
  23.                     int  line = vel.getLineNumber();  
  24.                     int  column = vel.getColumnNumber();  
  25.                     System.out.println();  
  26.                     System.err.println("At line "  + line +  ", column "  + column +  ": "  + msg);  
  27.                 }  
  28.             }  
  29.         }  
  30.           
  31.         return  obj;  
  32.     }  
public static Object unmarshal(InputStream xml, Class<?> clazz) throws SAXException {
		Object obj = null;
		
		ValidationEventCollector vec = new ValidationEventCollector();
		
		try {
			SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(new File("C://eclipse//workspace1//STAF//test//employees.xsd"));
			
			JAXBContext jc = JAXBContext.newInstance(clazz.getPackage().getName());
			Unmarshaller u = jc.createUnmarshaller();
			u.setSchema(schema);
            u.setEventHandler(vec);
			
			obj = u.unmarshal(xml);
		} catch (JAXBException e) {
			throw new RuntimeException("Can't unmarshal the XML file, error message: " + e.getMessage());
		} finally {
			if(vec != null && vec.hasEvents()) {
            	for(ValidationEvent ve : vec.getEvents()) {
            		String msg = ve.getMessage();
            		ValidationEventLocator vel = ve.getLocator();
            		int line = vel.getLineNumber();
            		int column = vel.getColumnNumber();
            		System.out.println();
            		System.err.println("At line " + line + ", column " + column + ": " + msg);
            	}
            }
		}
		
		return obj;
	}

 

该代码段适用于单一schema,或者在classpath中的多schema验证,如果是classpath外的多schema验证,请结合参考该文:XML validation for multiple schemas 验证使用多个XSD schema的XML文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值