分享

dom4j如何利用DTD进行Schema验证

 xue_dong5437 2011-03-11
public class XMLValidation {
    public static final int Monde_DTD = 0; // DTD形式验证

    public static final int Monde_Schema = 1; // Schema验证

    /**
     * 
     * @param xmlFile
     *            xml文件路径
     * @param validationFile
     *            校验文件路径
     * @param monde
     *            校验类型
     * @return 校验是否成功
     */
    public static boolean testXMLFile(String xmlFile, String validationFile,
            int monde) {
        boolean flag = true;
        if (monde == Monde_DTD) {// 如果是dtd校验调用校验dtd的(9php.com)方法
            flag = testXMLByDTD(xmlFile, validationFile);
        } else if (monde == Monde_Schema) {// 如果是xsd校验调用校验xsd的(9php.com)方法
            flag = testXMLByXsd(xmlFile, validationFile);
        } else {
            flag = false;
        }
        return flag;
    }

    /**
     * 校验 dtd 的(9php.com)方法
     * 
     * @param xmlFile
     *            xml文件路径
     * @param validationFile
     *            校验文件路径
     * @return 校验是否成功 此教程来源于97xxoo教程网(www.)查看完整的(9php.com)教程请点:http://www./article/4/2008/20081106262.shtml
     */
    private static boolean testXMLByDTD(final String xmlFile,
            final String validationFile) {
        /*
         * 此类实体包括在 DTD 内引用的(9php.com)外部 DTD
         * 子集和外部参数实体(无论哪种情形,仅在在解析器都读取外部参数实体时)和在文档元素内引用的(9php.com)外部通用实体(如果解析器读取外部通用实体)
         */
        EntityResolver resolver = new EntityResolver() {// 应用程序解析外部实体
            public InputSource resolveEntity(String publicId, String systemId) {
                InputStream is = null;
                try {
                    is = new FileInputStream(validationFile);// 读取dtd文档
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return null;
                }
                InputSource ins = new InputSource(is);
                ins.setPublicId(publicId);
                ins.setSystemId(systemId);
                return ins;// 返回 InputSource实例
            }
        };
        SAXReader reader = new SAXReader(true);
        reader.setEntityResolver(resolver); // 向SAX 驱动器注册一EntityResolver个实例。
        boolean flag = validate(xmlFile, reader);// 调用验证方法
        return flag;
    }

    /**
     * 验证 xsd 方法
     * 
     * @param xmlFile
     *            xml文件路径
     * @param validationFile
     *            校验文件路径
     * @return 校验是否成功
     */
    private static boolean testXMLByXsd(final String xmlFile,final String validationFile) {
        SAXReader reader = new SAXReader(true);// 创建SAXReader对象并制定需要验证
                                               // 也可通过reader.setValidation(true)来指定
        try {
            reader.setFeature("http:///sax/features/validation", true);// 设置功能标志的(9php.com)值name -功能名称,它是一个完全限定 URI。value - 请求的(9php.com)功能值(true 或false)。
            reader.setFeature("http:///xml/features/validation/schema", true);
            reader.setFeature("http:///xml/features/validation/schema-full-checking",true);
            reader.setProperty("http:///xml/properties/schema/external- noNamespaceSchemaLocation",validationFile);// 设置属性的(9php.com)值 name - 属性名称,它是一个完全限定 URI。value - 请求的(9php.com)属性值
        } catch (SAXException e) {
            e.printStackTrace();
            return false;// 如果捕获异常 则返回false
        }
        boolean flag = validate(xmlFile, reader);// 调用验证方法
        return flag;
    }

    /**
     * 
     * @param xmlFile xml文件路径
     * @param validationFile 校验文件路径 www.97xxoo5.cn
     * @param reader  SAXReader 对象
     * @return 校验是否成功
     */
    private static boolean validate(final String xmlFile, final SAXReader reader) {
        XMLErrorHandler errorHandle = new XMLErrorHandler();// 错误处理类实例
        reader.setErrorHandler(errorHandle);// 向 XML 阅读器注册一个实例
        File file = new File(xmlFile);
        InputStream is = null;
        if (file.exists() && file.isFile()) {
            try {
                is = new FileInputStream(file);// 读取xml
                InputStreamReader in = new InputStreamReader(is, "utf-8");
                reader.read(in);
            } catch (FileNotFoundException e) {// 如果出现异常返回false
                e.printStackTrace();
                return false;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return false;
            } catch (DocumentException e) {
                e.printStackTrace();
                return false;
            }
        } else
            return false;
        if (errorHandle.getErrors().hasContent()) {// 如果错误处理类实例中包含错误信息返回false;
            return false;
        }
        return true;
    }
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多