Unmarshal error in JAXB

心已入冬 提交于 2019-12-04 17:37:36

There are two ways to associate a class with a root element. The first is @XmlRootElement on the class, the second is an @XmlElementDecl annotation on a class annotated with @XmlRegistry (when the model is generated from an XML a Schema this class is called ObjectFactory.

Creating the JAXBContext

When you generate a model from XML Schema you should create the JAXBContext on the package name:

JAXBContext jc = JAXBContext.newInstance("demo");

Or generated ObjectFactory class:

JAXBContext jc = JAXBContext.newInstance(demo.ObjectFactory.class);

This will make sure the ObjectFactory class is processed.

No @XmlRootElement or @XmlElementDecl?

If there isn't an @XmlRootElement or @XmlElementDecl associating a class with the root element of your document then you will need to use one of the unmarshal methods that take a class parameter.

PersonType pt = unmarshaller.unmarshal(xml, PersonType.class).getValue();

The issue is that your PersonType class doesn't have @XmlRootElement. I think you'd want to do the "Unmarshalling into a known type" example in this page, which doesn't require @XmlRootElement on PersonType .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!