Use the following statements to
traverse a message tree from an element:
- 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.
- getChild()
- Returns the first child of the current element, whose name is
given by the first parameter. The nth occurrence of that
child element can be returned by specifying the second optional parameter.
- getChildren()
- Returns all the child elements of the current element as an array
of MbsElements. If the namespace parameter is
specified, the array contains only the child elements with that namespace
URI.
- 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 is created from the message. The message
has been sent by using WebSphere® MQ.
The logical tree diagram also shows the methods to call 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 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 that contains the message data "Some text.".
The following example shows how to use the MbsElement
methods to navigate to the chapter element:
$chapter = $input_assembly->getLastChild()->getFirstChild()->getFirstChild();
The
following example shows how to navigate to the chapter element by
using the path syntax:
$chapter = $input_assembly->XML->document->chapter;