Validating an XML Document with XOM
Validating an XML document is an important skill. XOM will activate it when you pass the true value to the nu.xom.Builder constructor as its second parameter. Any errors are reported through a nu.xom.ValidityException instance, which is a class that provides two methods for iterating/listing the occurred errors. These methods are:
-
public int getErrorCount(): This method returns the number of errors.
-
public String getValidityError(int n): This method returns the content of the specified error.
| Author's Note: Even if the document is not valid, it will be parsed anyway. |
Suppose you have the DTD document shown in Listing 4 for your AirWings_xml.xml document.
This document (AirWings_xml.dtd) should successfully validate AirWings_xml.xml, but as you can see, there are some errors in the XML document that should be reported when XOM checks its validity. To do this, run the example shown in Listing 5.
The output of this example is shown Figure 1.

Figure 1: Validation with XOM.
Passing a SAX Parser to XOM
Now suppose you want to pass a SAX parser to XOM. Follow these three simple steps:
- Create an
XMLReader object. The following is an example for a Xerces parser:
XMLReader xmlreader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
- Configure the
XMLReader instance. For example, to activate the XML Schema validation mechanism:
xmlreader.setFeature("http://apache.org/xml/features/validation/schema", true);
- Pass the
XMLReader to the Builder constructor:
Builder builder = new Builder(xmlreader, true);
That’s it! The SAX parser is now available!
Using XPath in XOM
Treated by XOM as a navigation solution (or alternative), XPath is the desirable way to query XML documents instead of using the old fashion navigation style. Again, XOM provides proof of simplicity by supporting complex XPath queries through a set of two methods named nu.xom.Node.query. Here are these methods' signatures:
-
public final Nodes query(String xpath): This method returns the nodes selected by the XPath expression in the context of this node in document order, as defined by XSLT (no namespace prefixes are allowed).
-
public final Nodes query(String xpath,XPathContext namespaces): This method returns the nodes selected by the xpath expression in the context of this node in document order, as defined in XSLT. The namespace should be bound to namespace URIs by the namespaces argument.
The nu.xom.XPathContext class allows you to bind prefixes and namespaces by calling its constructors:
public XPathContext(String prefix,String uri): This constructor creates a new XPath context that binds the specified prefix to the specified URI.
Listing 6 shows an example of using XPath in XOM.
Here’s an example of using the XPath queries with prefixed namespaces:
XPathContext context = new XPathContext("mc", "http://www.airwings.org/register/");
Nodes companydetails = doc.query("//mc:company-details", context);
Using XSLT with XOM
XOM offers support for XSLT in just a few steps, as follows:
- Load the document that you want to transform. For example:
Document input = builder.build(new File(".//AirWings_xml.xml"));
- Load a stylesheet from an XOM document:
Document stylesheet = builder.build(new File(".//AirWings_xslt.xsl"));
- Create an
nu.xom.xslt.XSLTransform object by passing the desired stylesheet:
XSLTransform transform = new XSLTransform(stylesheet);
- Call the
XSLTransform.transform method to apply the stylesheet. The result will be a set of nodes
represented by a nu.xom.Nodes instance:
Nodes output = transform.transform(input);
- Get the transformation result as a
nu.xom.Document:
Document result = XSLTransform.toDocument(output);
Listing 7 applies a stylesheet named AirWings_xslt.xsl to the AirWings_xml.xml.
Listing 8 shows the used XSLT (AirWings_xslt.xsl).
The result of this transformation is an HTML document named AirWings.html. This HTML will produce the output shown in Figure 2.

Figure 2: Applying XSLT transformation with XOM support.
Simplicity and High Performance
This introduction to XOM shows you an alternative to the existing solutions for processing XML documents. Though many other XOM skills were not revealed in this article, I hope that your curiosity will lead you to explore XOM and, in time, to use it in the production stage. Personally I value XOM for its simplicity and high performance and hope that there will soon a complete solution for processing XML based on the XOM engine.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|