advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : XML Made Easy with XOM :

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:
  1. Create an XMLReader object. The following is an example for a Xerces parser:
    XMLReader xmlreader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
    
  2. Configure the XMLReader instance. For example, to activate the XML Schema validation mechanism:
    xmlreader.setFeature("http://apache.org/xml/features/validation/schema", true);
    
  3. 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:
  1. Load the document that you want to transform. For example:
    Document input = builder.build(new File(".//AirWings_xml.xml"));
    
  2. Load a stylesheet from an XOM document:
    Document stylesheet = builder.build(new File(".//AirWings_xslt.xsl"));
    
  3. Create an nu.xom.xslt.XSLTransform object by passing the desired stylesheet:
    XSLTransform transform = new XSLTransform(stylesheet);
    
  4. 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);
    
  5. 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.

Home / Articles / XML Made Easy with XOM / 1 / 2 / 2 /

How to Add Java Applets to Your Site

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs