The Facts
In my java application I have to handle XML files with different schema versions (xsd files) simultaneously. The content of the XML files changed only a little between the different versions, so I'd like to use mainly the same code to handle it and just do some case distictions dependent on the version of the used schema.
Current Solution
Right now I'm parsing the XML files with a SAX parser and my own ContentHandler
ignoring the schema version and just checking if the tags I need for processing are present.
Possible Alternative
I'd really like to use JAXB to generate the classes for parsing the XML files. This way I could remove all the hardcoded strings (constants) from my java code and handle with the generated classes instead.
Question(s)
- How can I handle different schema versions in a unified way using JAXB?
- Is there a better solution?
Progress
I compiled the schema versions to different packages v1, v2 and v3. Now I can create an Unmarshaller
this way:
JAXBContext jc = JAXBContext.newInstance(
v1.Root.class, v2.Root.class, v3.Root.class );
Unmarshaller u = jc.createUnmarshaller();
Now u.unmarshal( xmlInputStream );
gives me the Root
class from the package matching the schema of the XML file.
Next I'll try to define an interface
to access the common parts of the schemas. If you have done something like this before, please let me know. In the mean time I'm reading through the JAXB specs...