John
John

Reputation: 13699

Why can't I convert an XML string to a JAXB element?

Here is the code that causes the problem.

String s = "<ns0:TestClass xmlns:ns0=\"http://www.w3.org/2001/XMLSchema\"><var>test string</var></ns0:TestClass>";
String xsdFile = "TestClass.xsd";

try {
    File xsdFileURL = new File(xsdFile);
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(xsdFileURL);

    JAXBContext jc = JAXBContext.newInstance(TestClass.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    unmarshaller.setSchema(schema);
    StringReader reader = new StringReader(s);

    //This is the line that causes the stacktrace
    JAXBElement<TestClass> root = (JAXBElement<TestClass>) unmarshaller.unmarshal(new StreamSource(reader), TestClass.class);

} catch (SAXException | JAXBException e) {
    e.printStackTrace();
    fail("Could not convert String to JAXB class.");
}

Here is the schema (TestClass.xsd).

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="TestClass">
    <xs:sequence>
      <xs:element name="var" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

This is the class I'm trying to convert.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TestClass", propOrder = {"var"})
class TestClass{

    @XmlElement(name = "var")
    protected String var;

    public String getVar() {
        return var;
    }

    public void setVar(String value) {
        this.var = value;
    }
}

This is the ObjectFactory.

@XmlRegistry
public class ObjectFactory {

    public ObjectFactory() {
    }

    public TestClass createTestClass() {
        return new TestClass();
    }

    @XmlElementDecl(namespace = "http://www.w3.org/2001/XMLSchema", name = "TestClass")
    JAXBElement<TestClass> createTestClass(TestClass value){
        QName qName = new QName("http://www.w3.org/2001/XMLSchema", "TestClass");
        return new JAXBElement<TestClass>(qName, TestClass.class, null, value);
    }

}

Here is the stacktrace that is spit out?

javax.xml.bind.UnmarshalException
 - with linked exception:
[Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred unmarshalling the document
Internal Exception: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 61; cvc-elt.1: Cannot find the declaration of element 'ns0:TestClass'.]
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:980)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:303)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at mockit.integration.junit4.internal.JUnit4TestRunnerDecorator.executeTestMethod(JUnit4TestRunnerDecorator.java:129)
    at mockit.integration.junit4.internal.JUnit4TestRunnerDecorator.invokeExplosively(JUnit4TestRunnerDecorator.java:74)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Why when I run this program does it tell me Cannot find the declaration of element 'ns0:TestClass'.]?

Upvotes: 1

Views: 1311

Answers (1)

f1sh
f1sh

Reputation: 11941

You declare the complexType in the xsd but you don't declare the element's usage.

Add a <xs:element name="TestClass" type="TestClass" /> with the correct namespaces before the xs:complexType so that the occurrence of the element is defined.

Upvotes: 1

Related Questions