Accessing elements in a message tree from a JavaCompute node

When you want to access the contents of a message, for reading or writing, use the structure and arrangement of the elements in the tree that the parser creates from the input bit stream. Follow the relevant parent and child relationships from the top of the tree downwards, until you reach the required element.

The message tree is passed to a JavaCompute node as an argument of the evaluate method. The argument is a MbMessageAssembly object. MbMessageAssembly contains four message objects:
  • Message
  • Local Environment
  • Global Environment
  • Exception List
These objects are read-only, except for Global Environment. If you try to write to the read-only objects, a MbReadOnlyException is thrown.
This topic contains the following information about accessing elements in a message tree:

Traversing the element tree

This table shows the Java methods that can be used to access element trees, and the equivalent ESQL correlation name for each point in the tree.
Java accessor from MbMessageAssembly ESQL correlation name
getMessage().getRootElement() InputRoot
getMessage().getRootElement().getLastChild() InputBody
getLocalEnvironment().getRootElement() InputLocalEnvironment
getGlobalEnvironment().getRootElement() Environment
getExceptionList().getRootElement() InputExceptionList
The following methods can be used to traverse a message tree from an element of type MbElement:
getParent()
returns the parent of the current element
getPreviousSibling()
returns the previous sibling of the current element
getNextSibling()
returns the next sibling of the current element
getFirstChild()
returns the first child of the current element
getLastChild()
returns the last child of the current element

The following example shows a simple XML message, and the logical tree that would be created from the message. The message has been sent using WebSphere MQ in this example. The logical tree diagram also shows the methods to call to navigate around the tree.

<document>
  <chapter title='Introduction'>
    Some text.
  </chapter>
</document>

This diagram shows the methods that you can call to navigate around a message tree. The tree used here is the one created by parsing the XML example given in this topic. From the Root part of the tree calling getFirstChild() navigates to Properties. Also from Root, calling getLastChild() returns XML. From Properties calling getParent() returns Root, and calling getNextSibling() returns MQMD. From MQMD calling getPreviousSibling() returns Properties, calling getParent() returns Root, and calling getNextSibling() returns XML. From XML calling getPreviousSibling() returns MQMD, calling getParent() returns Root, calling getFirstChild() returns document, and calling getLastChild() also returns document. From document calling getParent() returns XML, calling getFirstChild() returns chapter, and calling getLastChild() also returns chapter. From chapter calling getParent() returns document, calling getFirstChild() returns title, and calling getLastChild() returns the child containing the message data "Some text.".

The following Java code accesses the chapter element in the logical tree:
MbElement root = assembly.getMessage().getRootElement();
MbElement chapter = root.getLastChild().getFirstChild().getFirstChild();

Accessing information about an element using a JavaCompute node

Use the following methods to return information about the referenced element. The Java user-defined node API provides further detail about these methods:
getName()
Returns the element name as a java.lang.String
getValue()
Returns the element value
getType()
Returns the generic type, which is one of the following types:
  • NAME. An element of this type has a name, but no value.
  • VALUE. An element of this type has a value, but no name.
  • NAME/VALUE. An element of this type has both a value and a name.
getSpecificType()
Returns the parser-specific type of the element
getNamespace()
Returns the namespace URI of this element
Related information
Java user-defined node API