哪个xml验证器可以完美地用于多线程项目

Sun*_*hoo 7 java xml schema

我已经使用jdom对模式进行xml验证.主要的问题是它给出了一个错误

解析时可能无法调用FWK005解析

主要原因是多个线程同时用于xerces验证.所以我得到了解决方案,我必须锁定验证.这不好

所以我想知道哪个xml验证器适用于多线程项目

public static HashMap<String, String> validate(String xmlString, Validator validator) {

    HashMap<String, String> map = new HashMap<String, String>();
    long t1 = System.currentTimeMillis();
    DocumentBuilder builder = null;
    try {
        //obtain lock to proceed
//         lock.lock();

        try {
            builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//                Source source = new DOMSource(builder.parse(new ByteArrayInputStream(xmlString.getBytes())));

            validator.validate(new StreamSource(new StringReader(xmlString)));
            map.put("ISVALID", "TRUE");
            logger.info("We have successfuly validated the schema");
        } catch (Exception ioe) {
            ioe.printStackTrace();
            logger.error("NOT2 VALID STRING IS :" + xmlString);
            map.put("MSG", ioe.getMessage());
            //         logger.error("IOException while validating the input XML", ioe);
        }
        logger.info(map);
        long t2 = System.currentTimeMillis();
        logger.info("XML VALIDATION TOOK:::" + (t2 - t1));

    } catch (Exception e) {
        logger.error(e);
    } finally {
        //release lock
//         lock.unlock();
        builder = null;
    }

    return map;
}
Run Code Online (Sandbox Code Playgroud)

谢谢Sunil Kumar Sahoo

Dav*_*ave 5

我不认为任何java xml验证器都是线程安全的.选项是:

  1. 每次需要验证时创建一个新实例
  2. 创建一个您从中提取的验证器池
  3. 使用ThreadLocal来缓存验证器

  • +1 - 但第一个选项应该是每次需要时创建一个新的验证器.(现有选项是性能优化,只有在您确定*确认验证器实例的创建将成为性能瓶颈时才应考虑.) (3认同)