2017-07-17 365 views
0

我有以下代码:的TransformerFactory和Xalan依赖冲突

javax.xml.transform.TransformerFactory factory = TransformerFactory.newInstance(); 
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); 
javax.xml.transform.Transformer transformer = factory.newTransformer(); 

能正常工作正常。但是,我还需要在的Xalan我的pom.xml添加作为依赖,当我做的,上面的代码现在抛出一个错误:

java.lang.IllegalArgumentException: Not supported: http://javax.xml.XMLConstants/property/accessExternalDTD 

我认为这事做的事实,Xalan的罐子它有一个不同的Transformer实现。如何解决这个冲突而不改变上面的代码并保持Xalan作为依赖关系?从Xalan的

+0

为什么你需要的Xalan的依赖? Xalan自JDK 1.4以来一直得到认可。在正常情况下是没有必要的。 – fhossfel

+0

我正在制作一个Web应用程序,它对每个Java XML解析器都有单元测试。 – Dean

回答

1

排除Xerces的解决这个问题:如果你有多个XSL处理器和或不同版本

<dependency> 
    <groupId>xalan</groupId> 
    <artifactId>xalan</artifactId> 
    <version>2.7.2</version> 
    <exclusions> 
     <exclusion> 
      <groupId>xerces</groupId> 
      <artifactId>xercesImpl</artifactId> 
     </exclusion> 
    </exclusions> 
</dependency> 
0

,你必须处理,不是每一个实施将能够处理每一个属性的情况下。唯一的方法是捕获如果属性不受支持所引发的IllegalArgumentException。看看这个变更例中从JAXP documentation

javax.xml.transform.TransformerFactory factory = TransformerFactory.newInstance(); 

try { 
    factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); 
} catch (IllegalArgumentException e) { 
    //jaxp 1.5 feature not supported 
} 

文档说:

When code change is possible, and for new development, it is recommended that the new properties be set as demonstrated above. By setting the properties this way, applications can be sure to maintain the desired behavior whether they are deployed to older or newer version of the JDK, or whether the properties are set through System Properties or jaxp.properties.