package cn.com.agree.eci.ide.message.editor.presentation;

import java.io.InputStream;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

/**
*
* <DL><DT>
* 标题.
* </DT><p><DD>
* 详细介绍
* 验证xml的格式是否符合xsd的定义
* </DD></DL><p>
*
* <DL><DT><B>使用范例</B></DT><p><DD>
* 使用范例说明
* </DD></DL><p>
*
* @author dzh
* @author 赞同科技
* @version $Revision: 1.2 $ $Date: 2011-1-31$
*/
public class XmlValidator {
    public static final String XSD_RCD ="/schema/rcd.xsd";
    public static boolean validateXml(InputStream xml,InputStream xsd){
        try{
            final String schema_uri =XMLConstants.W3C_XML_SCHEMA_NS_URI;
            SchemaFactory factory =SchemaFactory.newInstance(schema_uri);
            Schema schema =factory.newSchema(new StreamSource(xsd));
            Validator val =schema.newValidator();
            val.validate(new StreamSource(xml));
        }catch(Exception e){
            //TODO log,将详细信息记录.log
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

posted on 2011-02-17 15:46  戴忠  阅读(1492)  评论(0编辑  收藏  举报