Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2350979
  • 博文数量: 321
  • 博客积分: 3440
  • 博客等级: 中校
  • 技术积分: 2992
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-24 09:08
个人简介

我就在这里

文章分类

全部博文(321)

文章存档

2015年(9)

2014年(84)

2013年(101)

2012年(25)

2011年(29)

2010年(21)

2009年(6)

2008年(23)

2007年(23)

分类: Java

2014-12-08 10:10:34


  1. package com;

  2. import java.io.ByteArrayInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;

  7. import javax.xml.XMLConstants;
  8. import javax.xml.stream.FactoryConfigurationError;
  9. import javax.xml.stream.XMLInputFactory;
  10. import javax.xml.stream.XMLStreamException;
  11. import javax.xml.stream.XMLStreamReader;
  12. import javax.xml.transform.Source;
  13. import javax.xml.transform.stream.StreamSource;
  14. import javax.xml.validation.Schema;
  15. import javax.xml.validation.SchemaFactory;
  16. import javax.xml.validation.Validator;

  17. import org.apache.axiom.om.OMElement;
  18. import org.apache.axiom.om.impl.builder.StAXOMBuilder;
  19. import org.xml.sax.SAXException;

  20. public class validateXML {

  21.     /**
  22.      * XSD和XML文件放在工程的bin路径下
  23.      */
  24.     public static boolean validate(String schemaLocaltion, OMElement request)
  25.             throws SAXException, IOException {
  26.         // 获取Schema工厂类
  27.         // 这里的XMLConstants.W3C_XML_SCHEMA_NS_URI的值就是://
  28.         SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  29.         // 获取XSD文件,以流的方式读取到Source中
  30.         // XSD文件的位置相对于类文件位置
  31.         Source schemaSource = new StreamSource(new FileInputStream(schemaLocaltion));

  32.         Schema schema = factory.newSchema(schemaSource);
  33.         // 这里是将一个DOM树对象转换成流对象,以便对DOM树对象验证
  34.         // 如果是对XML文件进行验证,用FileInputStream即可
  35.         String input = request.toString();
  36.         ByteArrayInputStream bais = new ByteArrayInputStream(
  37.                 input.getBytes("UTF-8"));
  38.         // 获取验证器,验证器的XML Schema源就是之前创建的Schema
  39.         Validator validator = schema.newValidator();

  40.         Source source = new StreamSource(bais);
  41.         // 执行验证
  42.         try {
  43.             validator.validate(source);
  44.             return true;
  45.         } catch (Exception ex) {
  46.             ex.printStackTrace();
  47.             return false;
  48.         } finally {
  49.             bais.close();
  50.         }
  51.     }

  52.     /**
  53.      * get OMElement soap request from specified XML file.
  54.      *
  55.      * @param request
  56.      * @return
  57.      * @throws FileNotFoundException
  58.      * @throws XMLStreamException
  59.      * @throws FactoryConfigurationError
  60.      */
  61.     public static OMElement getRequest(String filePath)
  62.             throws FileNotFoundException, XMLStreamException,
  63.             FactoryConfigurationError {
  64.         XMLStreamReader reader = XMLInputFactory.newInstance()
  65.                 .createXMLStreamReader(
  66.                         new FileInputStream(new File(filePath)));
  67.         StAXOMBuilder builder = new StAXOMBuilder(reader);
  68.         OMElement requestMessage = builder.getDocumentElement();
  69.         return requestMessage;
  70.     }

  71.     public static void main(String[] args) {
  72.         try {
  73.             if (validate("c:\\Person.xsd", getRequest("c:\\Person.xml"))) {
  74.                 System.out.println("customer_err.xml格式良好");
  75.             } else {
  76.                 System.out.println("customer_err.xml格式有误,请检查!");
  77.             }
  78.         } catch (Exception ex) {
  79.             ex.printStackTrace();
  80.             System.out.println("文件格式有误,请检查!");
  81.         }
  82.     }
  83. }
需要axiom.jar包
转自:http://longcxm.iteye.com/blog/1674674
阅读(1852) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~