jaxb 将xml转化为bean时报异常javax.xml.bind.UnmarshalException: unexpected element

前端之家收集整理的这篇文章主要介绍了jaxb 将xml转化为bean时报异常javax.xml.bind.UnmarshalException: unexpected element前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

异常详情

javax.xml.bind.UnmarshalException: unexpected element (uri:”“,local:”student”). Expected elements are (none)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:243)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:238)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext DefaultRootLoader.childElement(UnmarshallingContext.java:1048)atcom.sun.xml.internal.bind.v2.runtime.unmarshaller.Unmarshalling@H_502_293@Con@H_313_301@text.startElement(UnmarshallingContext.@H_386_403@java:483)atcom.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingCo@H_301_641@ntext.java:465)atcom.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135)atcom.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)atcom.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerI@H_403_1255@mpl.scanStartElemen@H_460_1301@t(XMLNSDocumentScannerImpl.java:379)atcom.sun@H_466_1403@.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:605)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3138)atcom.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl PrologDriver.next(XMLDocumentScannerImpl.java:880)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl JAXPSAXParser.parse(SAXParserImpl.java:648)atcom.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unmar@H_404_2180@shallerImpl.java:203@H_403_2218@)atcom.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unmars@H_748_2404@hallerImpl.java:175)atjavax.xml.bind.helpers.Ab@H_182_2502@stractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)atjavax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)atjavax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractU@H_404_3000@nmars@H_899_3010@h@H_360_3012@a@H_319_3014@l@H_8_3016@l@H_402_3018@erImpl.java:171)atjavax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)atcom.morris.xml.util.StudentTest.test(StudentTest.java:28)atsun.refl@H_99_3404@ect.NativeMethodAccessorImpl.invoke0(Nativ@H_675_3502@eMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)atjava.lang.reflect.Method.invoke(Method.java:606)atorg.j@H_690_4030@u@H_368_4032@n@H_940_4034@i@H_843_4036@t@H_183_4038@.@H_94_4040@r@H_367_4042@u@H_118_4044@n@H_570_4046@n@H_915_4048@ers.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 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
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)atorg.junit.runners.ParentRunner 1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access 000(P@H_518_4301@arentRunner.java:53)atorg.junit.runners.ParentRunn@H_209_4403@er 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)

解决办法

  1. Change the root element on your XML to be “student”
  2. Add the annotation @XmlRootElement(name=”student”) to the Student classs.

猜你在找的XML相关文章