2017-10-18 97 views
1

这是导致问题的代码。为什么我无法将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."); 
} 

这里是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> 

这是我试图转换的类。

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

+0

当你用'@ XmlRootElement''注解''TestClass''时它工作吗? – f1sh

+0

@ f1sh它没有。 – John

+0

您在xsd中声明complexType,但不声明该元素的用法。你可以在xs:complexType之前添加一个带有正确命名空间的''吗? – f1sh

回答

1

您在xsd中声明了complexType,但不声明该元素的用法。

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