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 arbitrary 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.