WebSphere Message Broker, Version 8.0.0.7
Operating Systems: AIX, HP-Itanium, Linux, Solaris, Windows, z/OS
See information about the latest product version
See information about the latest product version
Iterating with a filter
Use a FilterIterator to filter elements in the message tree.
The following example shows how to use a FilterIterator to iterate
over a message tree, extracting only the Toughened Steel components:
class ToughenedSteelPartsIterator extends FilterIterator
{
public function __construct(Iterator $it)
{
parent::__construct($it);
}
function accept()
{
/*** only check components ***/
if (!strcmp($this->key(),"component"))
{
/*** Return true for Toughened Steel ***/
if (!strcmp($this->current()->material,"Toughened Steel"))
{
return true;
}
}
return false;
}
}
class PhpComputeNode_Iterator_Filter
{
/**
* @MessageBrokerSimpleTransform
*/
function evaluate($output_assembly, $input_assembly)
{
ob_start();
echo "\nIterating over parts which have a material name of Toughened Steel\n";
$it = new MbsElementIterator($input_assembly->XMLNSC->getFirstChild());
$rii = new ToughenedSteelPartsIterator(new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST));
foreach($rii as $key=>$value)
{
echo $key.' = '.$value->name." ".$value->price." ".$value->material."\n";
}
$output_assembly->XMLNSC->results->contents = ob_get_contents();
ob_end_clean();
}
}
The following message is used as input to the FilterIterator shown
above:
<machine>
<assembly name="Engine Block Top End">
<assembly name="Cylinder Head">
<assembly name="Tappet Assembly">
<component name="Tappet" price="3.50" material="Toughened Steel"/>
<component name="Coil String" price="1.50" material="Spring Steel"/>
<component name="Circlip" price="1.50" material="Spring Steel"/>
<component name="Circlip" price="1.50" material="Spring Steel"/>
<component name="Seat" price="3.50" material="Toughened Steel"/>
</assembly>
<assembly name="Cam Shaft">
<component name="Shaft" price="3.50" material="Mild Steel"/>
<component name="Cam" price="1.50" material="Toughened Steel" />
<component name="Cam" price="1.50" material="Toughened Steel" />
<component name="Cam" price="1.50" material="Toughened Steel" />
<component name="Cam" price="1.50" material="Toughened Steel" />
<component name="Bearing" price="1.00" material="Toughened Steel" />
<component name="Bearing" price="1.00" material="Toughened Steel" />
<component name="Cam Chain Sprocket" price="4.20" material="Toughened Steel" />
<component name="Cam Chain" price="4.20" material="Toughened Steel" />
<component name="Tensioner" price="0.50" material="Plastic" />
</assembly>
<component name="Head" price="12.40" material="Alloy" />
<component name="Plug" price="3.00" material="Bakelite/Steel" />
</assembly>
<assembly name="Cylinder Block">
<component name="Cylinder" price="20.00" material="Alloy" />
<component name="Sleeve" price="10.00" material="Toughened Steel" />
</assembly>
<assembly name="Carburetor">
<assembly name="Piston Assembly"/>
<component name="Piston" price="20.00" material="Toughened Steel" />
<component name="Ring" price="10.00" material="Spring Steel" />
<component name="Ring" price="10.00" material="Spring Steel" />
<component name="Con Rod" price="10.00" material="Toughened Steel" />
</assembly>
</assembly>
</machine>
The resulting output shows only the parts whose material is Toughened
Steel:
<results><contents>
Iterating Over parts which have a material name of Toughened Steel
component = Tappet 3.50 Toughened Steel
component = Seat 3.50 Toughened Steel
component = Cam 1.50 Toughened Steel
component = Cam 1.50 Toughened Steel
component = Cam 1.50 Toughened Steel
component = Cam 1.50 Toughened Steel
component = Bearing 1.00 Toughened Steel
component = Bearing 1.00 Toughened Steel
component = Cam Chain Sprocket 4.20 Toughened Steel
component = Cam Chain 4.20 Toughened Steel
component = Sleeve 10.00 Toughened Steel
component = Piston 20.00 Toughened Steel
component = Con Rod 10.00 Toughened Steel
</contents></results>
For more information about the MbsElementIterator class, see MbsElementIterator.