Invalid JAXP API when unmarshaling JAXB

19.1k views Asked by At

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:

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.

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.

Running via Eclipse directly I get:

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)
6

There are 6 answers

0
samblake On BEST ANSWER

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.

0
Allan Chung On

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:

@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.

0
Rajesh Patil On

I solved this issue by using the following code. I am using JDK8 in my project

System.setProperty("javax.xml.transform.TransformerFactory", 
    "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); 
0
James On

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.

0
Jitendra Kumar On

I solved this problem by removing xalan from my project. I am using JDK7 in my project

1
Ondrej Bozek On

In my case it was caused by old version of xercesImpl. Dependency below fixed it

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