How to process different versions of XSD files in a Java application?

fact

In my java application, I have to process XML files of different schema versions (XSD files) at the same time The content of XML file only changes a little between different versions, so I want to mainly use the same code to deal with it, but make some case deformation according to the version of the schema used

Current solution

Now I use the Sax parser and my own content handler to parse the XML file, ignore the schema version, and just check whether there are tags I need to process

Possible options

I really want to use JAXB to generate classes that parse XML files In this way, I can remove all hard coded strings (constants) from my java code and use the generated classes to handle them

Questions

>How to use JAXB to handle different schema versions in a unified way? Is there a better solution?

progress

I compiled the schema version into different packages V1, V2 and v3 Now I can create an unmarshaller as follows:

JAXBContext jc = JAXBContext.newInstance( 
    v1.Root.class,v2.Root.class,v3.Root.class );
Unmarshaller u = jc.createUnmarshaller();

Now u.unmarshal (xmlinputstream); Give me the root class in the package that contains the schema matching the XML file

Next, I'll try to define an interface to access the common part of the pattern If you have done this before, please let me know At the same time, I'm reading the JAXB specification

Solution

First, you need some way to determine the schema that applies to a particular instance document You said that the document has a schemalocation attribute, so this is a solution Note, however, that you must specifically configure the parser to use this property, and malicious documents may specify schema locations beyond your control Instead, I recommend getting the attribute value and using it to find the appropriate schema in the internal table

The next step is to access the data You don't say why you use three different modes The only reasonable reason is the evolving data specification (i.e., the schema represents versions 1, 2 and 3 of the same data) If this is not your reason, then you need to rethink your design

If you want to support evolving data specifications, you need to answer the question "how to deal with lost data" There are several answers: one is to maintain multiple versions of code By refactoring common functionality, this is not a bad idea, but it can easily become unmaintainable

Another approach is to use a single code base and some type of adapter object that contains your rules If you go this way, JAXB is the wrong solution because it is related to an architecture You may be able to use an allowed XML - > java converter: I believe xStream will work, and I know that version 1.1 of practical XML will work (since I wrote it) – although you have to build it yourself

Another better option, depending on the complexity of the schema, is to develop a set of objects that use XPath to retrieve data I might use a "master" object containing an XPath expression in each variant of the pattern Then create a lightweight "wrapper" object that holds the DOM version of the instance document, and use XPath that is appropriate for the pattern Note, however, that this is limited to read - only access

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>