I create xml schemas based on my experience processing them in an automated fashion. Here are some pointers that make things easy for an automated xml consumer and also allow more robust management of (inevitable) schema changes:
When you write code to process xml based on a schema, use objects generated from the schema. This gives you protection from schema change in as much as a change in the schema and a rebuild of your code (including the code generation of course) will then indicate needed changes in your code with compile errors. This is a tremendous help, wherever possible look for compile time indication of errors rather than runtime errors after deployment!
For Java JDK 6 use the xjc tool to generate classes from a schema. Script it up say in Ant as part of your project build.
Designing an xml schema is like designing a relational database, and involves elements of object oriented design. Know these things before embarking on schema design!
Reuse types, both simple and complex. That way when you write code with generated objects you will be able to resuse code working on particular types.
Give maximum size limits to your data types, then when you design database fields to capture this information, provide bigger database field sizes than you initially need to accomodate change (e.g. put 50 chars down as a limit for a surname in the schema but use 100 chars in the database field). Remember, disk space is cheap having the same data in a varchar(100) won't take up more space than in a varchar(50). Somewhat larger field size limits won't slow your database down either.
Use lower case hyphenated element names (<start-time>) or upper camel case (<StartTime>) in the xml. Hyphenated names are still translated into object properties like "startTime" by tools such as xjc.
If an xml element has an associated unit, include the unit in the element name (either the SI unit abbreviation, or spell it out, e.g length-feet, mass-pounds). i.e rather than <time>54</time> use <time-ms>54</time-ms> or <time-milliseconds>54</time-milliseconds>. That way when someone produces xml using generated objects they will always know how to scale the value for the required unit (without having to check documentation for the field).
Thursday, November 29, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment