2

I'm integrating on a Large Enterprise Platform and the vendor likes to update their XML format frequently, although they have promised that changes will continue to be backwards-compatible. I have an XSD that works...now...but I'd like to deploy that schema with our application code and not have to redeploy for every web service update. As an additional impetus, the (JVM-based) configuration language has very nice XSD type importing.

As a fallback, I can extract specific elements using XPath queries, but that's much less pleasant.

  • How can I continue to parse evolving XML against a schema file that is outdated but not obsolete?

I'm looking for something like a 'loose parse' option or 'ignore unknown tags' that will get me access to the parts of the document that our application currently knows and cares about. Any new tags can be discarded - from the business standpoint, they're irrelevant.

gws
  • 459
  • 1
  • 7
  • 16

1 Answers1

4

This is the default behavior of JAXB (Java Architecture for XML Binding) which is a Java standard for defining how Java objects are converted from and to XML.

If you are starting with an XSD provided by someone else, you can generate classes using the xjc tool that comes with the JDK.

> xjc vendor.xsd

see how-to-generate-jaxb-classes-from-xsd

By default, this will created a 'generated' package of classes representing the elements defined in the XSD. If the top element of the XML was VendorDocument the following code will let you play with the input file:

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import generated.VendorDocument;

public class VendorMain {

  public static void main(String[] args) throws JAXBException, IOException {

    // create JAXB context
    JAXBContext context = JAXBContext.newInstance(VendorDocument.class);

    // unmarshal document from file
    Unmarshaller um = context.createUnmarshaller();
    VendorDocument vendorDoc = (VendorDocument) um.unmarshal(new FileReader("vendor.xml"));


    //Check what got read in by writing it out.  Will not have unknown tags.
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(vendorDoc, new File("output.xml"));

  }
}

You will see that if you add extra tags and attributes to the vendor.xml input file, they are simply ignored and you can continue to get to the parts of the document you need. As long as things are only added to the XSD in the future and not removed you shouldn't have a problem.

The code snippet above was adapted from this tutorial.

Community
  • 1
  • 1
nfdavenport
  • 475
  • 1
  • 4
  • 10