XML Validation

An XML Validator

To help you check the syntax of your XML files, we have created an XML validator to syntax-check your XML.

Valid XML Documents

A valid XML document is not the same as a well formed XML document.

The first rule, for a valid XML document, is that it must be well formed (see previous paragraph).

The second rule is that a valid XML document must conform to a document type.

Rules that defines legal elements and attributes for XML documents are often called document definitions, or document schemas.

When to Use a Document Definition?

A document definition is the easiest way to provide a reference to the legal elements and attributes of a document.

A document definition also provides a common reference that many users (developers) can share.

A document definition provides a standardization that makes life easier.

When NOT to Use a Document Definition?

XML does not require a document definition.

When you are experimenting with XML, or when you are working with small XML files, creating document definitions may be a waste of time.

If you develop applications, wait until the specification is stable before you add a document definition. Otherwise, your software might stop working because of validation errors.

Document Definitions

There are different types of document definitions that can be used with XML:

  • The original Document Type Definition (DTD)
  • The newer, and XML based, XML Schema

XML Elements vs Attributes

Usually elements are used to capture the data however attributes can be to capture the metadata (data about data – something that provides additional information about the data) ie the data which might not be used to display but can be used for search or other such features.There is no rule as such that says something should be an attribute or a element. I recommend to avoid it Winking smile

Some of the problems with using attributes are:

  • attributes cannot contain multiple values (elements can)
  • attributes cannot contain tree structures (elements can)
  • attributes are not easily expandable (for future changes)

Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data.

The following three XML documents contain exactly the same information:

A date attribute is used in the first example:

<note date=”10/01/2008″>
<body>Don’t forget me this weekend!</body>

A date element is used in the second example:

<body>Don’t forget me this weekend!</body>

An expanded date element is used in the third: Preferred :

<body>Don’t forget me this weekend!</body>

Hibernate : how to create database tables automatically

When you are dealing with the database as a java programmer, you need not to have data table creation scripts for various databases. Hibernate creates the database tables for you automatically based on the entity relations ships and associations if you configure that way.

The most simple way to configure it is below tag in hibernate.cfg.xml
<property name=”hbm2ddl.auto”>create</property>

So the list of possible options are,

  • create-drop: executes SchemaExport when SessionFactory initializes and drops the schema at the end of the life of the factory.
  • create: creates the schema, destroying previous data.
  • update: update the schema according to the changes made in the hibernate entities. eg if some columns/mappings/associations are added or droped.
  • validate: validate the schema, makes no changes to the database. if the schema is not up to date to run hibernate program, it will through an error

NOTE : Despite folks in Hib do their best, you simply cannot rely on automatic updates in production. Write you own patches, review them with DBA, test them, then apply them manually.