title: XercesImpl in conflict with JavaSE 6's internal xerces implementation. Both are needed... what can be done?
body: <p>I am sure that I am not the first to encounter this conflict.</p>
  
  <p>The code that I have inherited does the following:</p>
  
  <pre><code>org.w3c.dom.Document dom; // declaration
  javax.xml.validation.Schema schema; // declaration
  
  ...
  ...
  ...
  
  javax.xml.validation.Validator validator = schema.newValidator();
  validator.validate(new DOMSource(dom));
  </code></pre>
  
  <p>where the <code>...</code> stands for seemingly unimportant/irrelevant code</p>
  
  <p>Compiling and running the code with JDK 6 works (and always had...)</p>
  
  <p>Recently I have had to integrate into my code another component written elsewhere in the company. That component absolutely requires the inclusion in the classpath of <code>xercesImpl-2.8.1.jar</code></p>
  
  <p>I absolutely require this 3rd party component, <em>but</em> now running the code above no longer works and I get the following:</p>
  
  <pre><code>org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'Root'.
   at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
   at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
   at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
   at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
   at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
   at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
   at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
   at org.apache.xerces.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
   at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
   at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
   at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
   at javax.xml.validation.Validator.validate(Validator.java:127)
  </code></pre>
  
  <p>As a solution, I have thought perhaps somehow to shield the xercesImpl-2.8.1.jar in a classloader of its own, but have not managed to do so, perhaps due to lack of classloader knowledge or perhaps because its not the way to go. One more thing about my environment, my app runs on tomcat 5.5 and 6...</p>
  
  <p>by the way while debugging I have noticed that when I run <code>dom.getImplementation()</code></p>
  
  <ul>
  <li>when adding the
  <code>xercesImpl-2.8.1.jar</code> to the
  classpath the result is
  <code>org.apache.xerces.dom.DeferredDOMImplementationImpl@5f15c</code></li>
  <li>when removing it the result is <code>com.sun.org.apache.xerces.internal.dom.DeferredDOMImplementationImpl@6c6ae3</code></li>
  </ul>
  
  <p><em>[No surprise to you careful readers I suppose]</em></p>
  
  <p>Any suggestions?</p>