2017-03-16 79 views
0

我的任务是提取给定复杂类型中每个可选属性的(名称,默认值)对。 我有下面的代码:当使用XSAttributeUse.getValueConstraintValue()时,Apache Xerces抛出java.lang.NoSuchMethodError

XSObjectList attrList = typeDefinition.getAttributeUses(); 
for (int i = 0; i < attrList.getLength(); i++) { 
    XSAttributeUse attributeUse = (XSAttributeUse) attrList.item(i); 

    if (!attributeUse.getRequired()) { 
     String name = attributeUse.getValueConstraintValue().getNormalizedValue(); 
     String value = /* extracting the default value */; 
    } 
} 

这会产生错误:

Exception in thread "main" java.lang.NoSuchMethodError: org/apache/xerces/xs/XSAttributeUse.getValueConstraintValue()Lorg/apache/xerces/xs/XSValue; (loaded from path\to\sdk\jre\lib\xml.jar by <Bootstrap Loader>) called from class my.package.xsd.XSDParser (loaded from file:/path/to/the/project/SwidSigner/target/classes/ by [email protected]). 
    at my.package.xsd.XSDParser.getTypeAttributes(XSDParser.java:54) 
    at my.package.xsd.XSDParser.getAttributes(XSDParser.java:37) 
    at my.package.Main.main(Main.java:29) 

更重要的是,当我将其更改为:

XSObjectList attrList = typeDefinition.getAttributeUses(); 
for (int i = 0; i < attrList.getLength(); i++) { 
    XSAttributeUse attributeUse = (XSAttributeUse) attrList.item(i); 
    XSAttributeDeclaration attributeDecl = attributeUse.getAttrDeclaration(); 

    if (!attributeUse.getRequired()) { 
     String name = attributeDecl.getValueConstraintValue().getNormalizedValue(); 
     String value = /* extracting the default value */; 
    } 
} 

仍然出现错误,但它反而抱怨XSAttributeDeclaration类。使用其他方法时,如

XSComplexTypeDefinition.getAttributeUses() 

我使用Maven导入的Xerces没有错误:

<dependency> 
    <groupId>xerces</groupId> 
    <artifactId>xercesImpl</artifactId> 
    <version>2.11.0</version> 
</dependency> 
+1

您使用的是什么版本的Java?它看起来像试图从你的JRE的lib目录中加载一个Xerces类。 – rmlan

+0

我正在使用Java 8.我如何使它从Maven加载Xerces类? –

回答

1

你必须在它Xerces类流氓罐子。

检查您安装的JRE的lib目录。它似乎是从名为xml.jar的jar加载有问题的类。我不确定这个jar是从哪里来的,因为我没有在我目前正在使用的任何Java安装中看到它(在Linux和OSX上的Java 8)。

如果删除(或移动)该jar,应该解决该问题。请注意,这可能会对系统上的其他Java程序产生意想不到的后果。

1

NoSuchMethodError意味着您的编译时类路径中存在的代码不存在于运行时类路径中。检查是否有差异。如果您使用的是为您生成“导入”的IDE,请检查它是否导入了您期望使用的软件包。

相关问题