Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Using the JMS API and XML in Content-Based Routing

By Todd Sundsted
2003-12-20


XML and XPath

XML is, as I'm sure most of you know, an excellent language for representing many kinds of structured information. Its use as a message format allows us to leverage an existing body of XML-related technology to implement the rule and routing functions.

XPath notation
Although XPath has been a W3C recommendation since November 1999, most people aren't familiar with its role and syntax. Therefore, I'll stop for a moment to explain its use.

Recall that an XML document is logically a tree of elements. Because it's acyclic, starting from the root node of the tree, there is only one path to any other node of the tree. XPath provides a simple, clean language for specifying and selecting subsets of the nodes in the tree based on the path to those nodes from the root.

The following examples, while not nearly indicative of the power of XPath, will give you a feel for the syntax:

  • Select all elements of type CHILD that are children of the root element ROOT: /ROOT/CHILD
  • Select all elements of type NODE that appear anywhere in the XML document: //NODE
  • Select the first element of type CHILD that is a child of the root element ROOT: /ROOT/CHILD[1]
  • Select all elements of type NODE that have an attribute named attr: //NODE[@attr]
  • Select all elements of type NODE that have an attribute named attr with the value 'x': //NODE[@attr='x']
  • Select all elements the name of which starts with the letter 'X': //*[starts-with(name(),'X')]

To route the messages that arrive at the router, the router needs information in the form of rules that specify who is interested in what types of information.

The simplest technology for useful rules might be regular expressions. However, since the messages use XML, a better solution is to use XPath notation. The result is almost as easy to implement.

Let's consider a typical rule. A router rule comprises two pieces of information: an XPath string to be matched against received XML documents, and a destination specifying where to send the XML document if the XPath expression is satisfied.

The router applies the rules it has on hand to the XML documents it receives, and passes those that match to the appropriate destination.



Tutorial Pages:
» How to make sure information gets to the right people
» Messaging and the JMS API
» A simple content-based routing system with agents
» XML and XPath
» The code
» Conclusion
» Resources


IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT