Configuring the Xerces XML Parser With Content Model Defaults

My previous post on JSON schema included a slight dig at XML, which perhaps wasn't really warranted. True, XML is clunkier and more verbose than JSON, but it has its strong points. The clincher for me in past projects has been the superior expressiveness of XML's schema format: XSD. XSD has quite a few capabilities that JSON schema lacks, such as referential integrity constraints, and the ability to specify attribute defaults in one's content model. This article provides a quick overview of the latter feature via a code sample that illustrates how to configure Apache Xerces. The configuration we present enables you to read in XML content such that it is auto-populated with the proper default values for attributes, even if the original source XML does not contain any definition at all for those attributes.

Why is this useful? Well, suppose you have developed a content model that allows your users to configure some run time data. For example, say you have a game with actors that can be animals or people. You define an XSD (schema) which allows game configurers to define a cast of characters for the game using XML, like this:

Reducing Integration Hassles With JSON Schema Contracts

I recently worked on a project where the 'contract' between service consumers and providers consisted primarily of annotated mock-ups of the JSON responses one would obtain from each of a given service's end-points. A much better way of expressing the contract for a service is to use a standard schema format. If you're stuck with XML, use XML schema. If you are using JSON, there are tools and libraries (presented below) that will help you use JSON schema to express a service's contract.

This article will assume that you have gone through the available JSON schema documentation and have a basic idea of how to use it. It assumes that you are developing on a JVM-based platform, and most of the recipes will be helpful for Java developers (although our example of dynamic schema validation is presented using a bit of Scala.)