Accessing elements in a message tree from a JavaCompute node
Access the contents of a message, for reading or writing, using the structure and arrangement of the elements in the tree that the parser creates from the input bit stream.
About this task
Follow the relevant parent and child relationships from the top of the tree downwards until you reach the required element.
- Message
- Local Environment
- Global Environment
- Exception List
Alternatively, use JAXB Java™ objects with getter and setter methods; see Using JAXB with a JavaCompute node.
Traversing the element tree
About this task
| Java accessor from MbMessageAssembly | ESQL field type constant |
|---|---|
| getMessage().getRootElement() | InputRoot |
| getMessage().getRootElement().getLastChild() | InputBody |
| getLocalEnvironment().getRootElement() | InputLocalEnvironment |
| getGlobalEnvironment().getRootElement() | Environment |
| getExceptionList().getRootElement() | InputExceptionList |
Alternatively, use a Document Object Model (DOM) object, and use the DOM API to navigate and manipulate the elements.
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. The logical tree diagram also shows the methods to call in order to navigate around the tree.
<document>
<chapter title='Introduction'>
Some text
</chapter>
</document>

- 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 that contains the message data "Some text.".
MbElement root = assembly.getMessage().getRootElement();
MbElement chapter = root.getLastChild().getFirstChild().getFirstChild();