为什么我不能将XML字符串转换为JAXB元素?

时间:2017-10-18 20:04:48

标签: java xml jaxb

以下是导致问题的代码。

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.");
}

这是架构(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>

这是我要转换的课程。

@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;
    }
}

这是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);
    }

}

这是吐出的堆栈跟踪吗?

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)

为什么我运行此程序时会告诉我Cannot find the declaration of element 'ns0:TestClass'.]

1 个答案:

答案 0 :(得分:1)

您在xsd中声明了complexType,但是您没有声明元素的用法。

<xs:element name="TestClass" type="TestClass" />之前添加具有正确名称空间的xs:complexType,以便定义元素的出现。