The following table shows the Java methods that you can use to
access element trees, and the equivalent ESQL field type constant
for each point in the tree.
Java accessor
from MbMessageAssembly |
ESQL field type constant |
getMessage().getRootElement() |
InputRoot |
getMessage().getRootElement().getLastChild() |
InputBody |
getLocalEnvironment().getRootElement() |
InputLocalEnvironment |
getGlobalEnvironment().getRootElement() |
Environment |
getExceptionList().getRootElement() |
InputExceptionList |
Use the following methods 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
Alternatively, use a Document Object
Model (DOM) object, and use the DOM API to navigate and manipulate
the elements.
MBMessage provides the following methods:
- getDOMDocument()
- obtains a W3C org.w3c.dom.Document object for
the message body.
- createDOMDocument()
- obtains a W3C org.w3c.dom.Document object for
the message body.
MbElement provides the following
method:
- getDOMNode()
- obtains a W3C org.w3c.dom.Node object for the message 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. 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>
The tree
used in this diagram is the one that is created by parsing the previous
XML example.
- 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.".
The following Java code
accesses the chapter element in the logical tree for an XML message
that does not contain white spaces. The XML parser retains white space
in the parsed tree, but the XMLNS and XMLNSC parsers do not.
MbElement root = assembly.getMessage().getRootElement();
MbElement chapter = root.getLastChild().getFirstChild().getFirstChild();