tags:

views:

41

answers:

1

Hi All, I have used jdom for xml validation against schema. The main problem there is that it gives an error

FWK005 parse may not be called while parsing

The main reason was that multiple of threads working for xerces validation at the same time. SO I got the solution that i have to lock that validation. which is not good

So I want to know which xml validator works perfectly for multithreading project

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;
}

Thanks Sunil Kumar Sahoo

+1  A: 

I don't think any of the java xml validators are threadsafe. Options are to:

  1. Create a pool of validators that you pull from
  2. Make use of ThreadLocal to cache validators
Dave
+1 - but the first option should be simply to create a new validator each time you need one. (The existing options are performance optimizations, and should only be considered if you *know for sure* that creation of validator instances will be a performance bottleneck.)
Stephen C
Very good point above from Stephen C.
Dave