JAXB Unmarshall异常 - 意外元素

Vít*_*ega 7 exception jaxb unmarshalling

我使用.xsd文件生成Java类,并且使用XML文件,我需要解组.

我正在使用此代码:

JAXBContext objJAXBContext = JAXBContext.newInstance("my.test");

// create an Unmarshaller
Unmarshaller objUnmarshaller = objJAXBContext.createUnmarshaller();

FileInputStream fis = new FileInputStream("test.xml");

JAXBElement<Root> objMyRoot = (JAXBElement<Root>) objUnmarshaller.unmarshal(fis);

Root mRoot = objMyRoot.getValue();
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Root"). Expected elements are (none)
Run Code Online (Sandbox Code Playgroud)

我见过很多解决方案,但在我的项目中没有任何作用.

我可以尝试做什么?

Waq*_*mon 16

您的xml根目录缺少名称空间(uri)属性.你最好试试这个XMLRootElement......

@XmlRootElement(name = "root", namespace="")
Run Code Online (Sandbox Code Playgroud)