java - 使用 XSAttributeUse.getValueConstraintValue() 时,Apache Xerces 抛出 java.lang.NoSuchMethodError

标签 java xml xsd xerces 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 sun.misc.Launcher$AppClassLoader@29b444c2).
    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>

最佳答案

您有一个流氓 jar,其中包含 Xerces 类。

检查已安装的 JRE 的 lib 目录。看起来它正在从名为 xml.jar 的 jar 中加载相关类。我不确定这个 jar 来自哪里,因为我在当前使用的任何 Java 安装中都没有看到它(Linux 和 OSX 上的 Java 8)。

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

关于java - 使用 XSAttributeUse.getValueConstraintValue() 时,Apache Xerces 抛出 java.lang.NoSuchMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42832944/

相关文章:

java - 如何使用 xsd 验证 xml?

java - Jetty 的 XML 模式错误

java - jaxb base64二进制编码/解码

java - 从 500mb 文件中提取多个 XML

java - 如何检查2个类实例是否相同

java - Intent (上下文,类)不起作用当我传入类对象时给出错误

java - 循环,编程新手

java - Android xml 中的嵌套数组

android - 在包“android”中找不到属性 'contentDescription' 的资源标识符

java - 非法选项: -cacerts for keytool in gitlab-ci with gradle:jdk8