开发者

JAXB: How to know which Java class to unmarshal to when there are multiple XSDs?

I have several XSD files which I have processed using XJC to generate Java classes.

I would like to read an arbitrary XML file, and based on the contents of the XML file, convert it to one of the generated Java class.

Is it possible to do this without examining the contents of the XML itself, and letting JAXB framework do all the work?

For example, let's say I have the following XSD files:

dog.xsd cat.xsd

I compile them using XJC to generate Dog.java and Cat.java.

I now read some arbitr开发者_如何学运维ary XML, and create an instance of either Dog or Cat, depending on which schema it matches.

Thanks.


XJC generates classes based on the types in your schemas, not the schemas themselves. And it puts the generated classes into packages based on that target namespaces. If you load multiple schemas into the JAXBContext, it will automatically unmarshall to the correct type. That's pretty much the whole point of JAXB. If your two schemas have types with the same names in the same namespace, XJC would fail.


A simple solution that might work is try to unmarshall one of them, if it goes ok, then its the right one, if you get an exception that its the second one you should use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜