繁体   English   中英

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

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

这是导致问题的代码。

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

这是对象工厂。

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

您在 xsd 中声明了 complexType 但未声明元素的用法。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM