There are wide usages of XML in the representation of arbitrary data structures such as those used in web service. XML and its extensions are often criticized for verbosity and complexity. There are other akternatives such as JSON, YAML, and S-Expression which concentrate on representing highly structured data. Here, I'm gonna briefly explain XML and demonstrate how to use Java to validate XML with a XML schema. Code can be retrieved at xml and java code
XML and XML schema brief
XML (extensible markup language) is a markup language which is a system for annotating a document in a way that is syntactically distinguishable from the text and defines some rules for formatting documents in human-readable and machine-readable formats. The markup can be transformed into HTML, PDF, and Rich Text Format using a programming language or XSL.
The XML specification defines an XML document as a well-formed text when the following conditions are met:
properly encoded unicode characters.
< and & appear only when markup.
Elements are correctly nested without overlapping.
Tags are case-sensitive.
There's only a single root element that contains all the other elements.
Here's a simple XML document called note.xml
A valid XML document as a well-formed XML document also contains a reference to a DTD(Document Type Definition) or XML schema. Here's an example of XML schema called note.xsd that defines the elements of the XML document above.
Java code
Here, I use dom4j-1.6.1 as the SAX (Simple API for XML) reader which is an event-driven for parsing XML. SAX that operates on each piece of the XML document sequentially is an alternatives to DOM (Document Object Model) that operates on the document as a whole.
Specify the XML document and XML schema.
Create SAX factory to obtain and set a SAX parser and schema factory to gain and configure a XML schema parser.
Set the schema for the SAX parser and create the SAX reader.
Create the callback function for the SAX reader so that everytime a SAX error occurs, the error would be printed out.
Set the callback functions and read a XML file.
Full piece of code is shown below.
Demonstration
Download the jar or use maven to manage the jar of dom4j
Compile the java code with the specufied class path (the dom4j jar downloaded)
Run the program with the specified path to te dom4j jar
Replace the note.xml with a invalid content that change the "to" tag to the "for" tag.
Run the program again and the error would be shown.
Conclusion
There are many alternatives in this piece of code, such as multiple source of schema, different schema language, and customized error handlers. To make full use of the javax.xml and dom4j, please reference the links below to explore the full power of validating XML using Java.