Java 解组 JAXB 时无效的 JAXP API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18493541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invalid JAXP API when unmarshaling JAXB
提问by samblake
I have a JAXB object I can unmarshal fine. However, if I add a Element[] field annotated with @XmlAnyElement, when attempting to unmarshal it throws:
我有一个可以解组的 JAXB 对象。但是,如果我添加一个用@XmlAnyElement 注释的 Element[] 字段,则在尝试解组时会抛出:
java.lang.IllegalStateException: You are running with invalid JAXP api or implementation. JAXP api/implementation of version 1.3.1 (included in JDK6) or higher is required. In case you are using ant, make sure ant 1.7.0 or higher is used - older versions of ant contain JAXP api/impl version 1.2 (in xml-apis.jar). If you want to keep using older ant versions, you have to configure it to use higher the JAXP api/impl versions.
java.lang.IllegalStateException:您正在使用无效的 JAXP api 或实现运行。需要 1.3.1 版(包含在 JDK6 中)或更高版本的 JAXP api/实现。如果您使用 ant,请确保使用 ant 1.7.0 或更高版本 - 旧版本的 ant 包含 JAXP api/impl 1.2 版(在 xml-apis.jar 中)。如果您想继续使用较旧的 ant 版本,则必须将其配置为使用更高的 JAXP api/impl 版本。
This is run from a TestNG test that I am running via the Eclipse plugin. I am running with the 1.6 JDK and configured maven to exclude old versions of the xml-apis jar however I can't seem to get it working. I assume I'm still picking up incompatible versions of the libraries from somewhere but don't know where from.
这是从我通过 Eclipse 插件运行的 TestNG 测试运行的。我正在使用 1.6 JDK 并配置 maven 以排除旧版本的 xml-apis jar,但是我似乎无法让它工作。我想我仍然从某个地方获取不兼容版本的库,但不知道从哪里来。
Running via Eclipse directly I get:
直接通过 Eclipse 运行我得到:
29-Aug-2013 10:04:08 com.sun.xml.bind.v2.util.XmlFactory createTransformerFactory
SEVERE: null
java.lang.AbstractMethodError: javax.xml.transform.TransformerFactory.setFeature(Ljava/lang/String;Z)V
at com.sun.xml.bind.v2.util.XmlFactory.createTransformerFactory(XmlFactory.java:155)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.createTransformerHandler(JAXBContextImpl.java:747)
at com.sun.xml.bind.v2.runtime.unmarshaller.DomLoader$State.<init>(DomLoader.java:75)
at com.sun.xml.bind.v2.runtime.unmarshaller.DomLoader.startElement(DomLoader.java:118)
at com.sun.xml.bind.v2.runtime.unmarshaller.ProxyLoader.startElement(ProxyLoader.java:60)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:501)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:480)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:150)
at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
at org.apache.xerces.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:218)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:190)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)
at com.gtspt.vrs.parser.AbstractParserTest.unmarshal(AbstractParserTest.java:33)
at com.gtspt.vrs.parser.AbstractParserTest.testParser(AbstractParserTest.java:26)
at com.gtspt.vrs.parser.TestResultsParser.testParser(TestResultsParser.java:17)
at com.gtspt.vrs.parser.Test.main(Test.java:16)
采纳答案by samblake
It looks like the issue is due to an old version of Xalan. It seems a minimum of version 2.7.0 is needed since JDK 5. Another library was pulling in an older version so I excluded that from the Maven build and everything looks good now.
看起来问题是由于旧版本的 Xalan。从 JDK 5 开始,似乎至少需要 2.7.0 版本。另一个库正在使用旧版本,所以我从 Maven 构建中排除了它,现在一切看起来都很好。
回答by James
In addition/support and to be more specific, if you are running a maven or JPA project you should be able to right click on the dependency directory, add dependency, and search for xalan. Add any versions 2.7.0 and up under the xalan:xalan list.
此外/支持,更具体地说,如果您正在运行 maven 或 JPA 项目,您应该能够右键单击依赖项目录,添加依赖项并搜索 xalan。在 xalan:xalan 列表下添加任何 2.7.0 及更高版本。
回答by Ondrej Bozek
In my case it was caused by old version of xercesImpl
. Dependency below fixed it
就我而言,它是由旧版本的xercesImpl
. 下面的依赖修复了它
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
</dependency>
回答by Allan Chung
I ran into the same error with the exact same wordings, but it turned out to be a totally different problem than dependency problem like the error suggested. So I am sharing my encounter here in case someone out there could also benefit.
我用完全相同的措辞遇到了同样的错误,但结果证明它是一个完全不同的问题,而不是像建议的错误那样的依赖问题。所以我在这里分享我的遭遇,以防有人也能从中受益。
I had something like these in my code:
我的代码中有这样的东西:
public class ImageObject {
@XmlElementWrapper(name="shapes")
@XmlAnyElement(lax=true)
@XmlElementRefs({
@XmlElementRef(name = "circle", type=Circle.class),
@XmlElementRef(name = "square", type=Square.class),
@XmlElementRef(name = "rectangle", type=Rectangle.class),
@XmlElementRef(name = "triangle", type=Triangle.class)
})
private List<Object> shapes;
...
}
And in the Rectangle class, I had a typo in element name specified for the @XmlRootElement - "retangle", not matching the name "rectangle"inside the @XmlElementRef in the ImageObject class:
在 Rectangle 类中,我在为 @XmlRootElement - "retangle"指定的元素名称中有一个拼写错误,与 ImageObject 类中 @XmlElementRef 中的名称"rectangle"不匹配:
@XmlRootElement(name = "retangle")
public class Rectangle extends Shape {
@XmlAttribute
private String colour;
@XmlAttribute
private String width;
...
}
So, watch out for this too if you are using the @XmlElementRef annotation.
因此,如果您使用 @XmlElementRef 注释,也要注意这一点。
回答by Jitendra Kumar
I solved this problem by removing xalan from my project. I am using JDK7 in my project
我通过从我的项目中删除 xalan 解决了这个问题。我在我的项目中使用 JDK7
回答by Rajesh Patil
I solved this issue by using the following code. I am using JDK8in my project
我使用以下代码解决了这个问题。我在我的项目中使用JDK8
System.setProperty("javax.xml.transform.TransformerFactory",
"com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");