|
XML Processing using the Whitebeam Environment
|
Manually Executing XML SubtreesThe XML tree built with the XmlParser.build
is generally 'executed' in order to populate the 'output' area of each element. It is not 'required' that you execute the XML -
there are applications that may just require the basic structure of the tree. At any time the output area may contain different things:
You can see from the description of the rb:block
tag above that initially the output areas of all children of
the block will be empty. In JavaScript you can however get hold of the sub-tree of the rb:block tag and execute it.
To do this give the block tag an id (using the rb:id attribute). This is illustrated below:
<rb:block rb:id="myBlock">
<tr>
<td>
<rb:eval expr="prop">
</td>
<td>
<rb:eval expr="object[prop]">
</td>
</tr>
</rb:block>
This example block is labeled - so we can find it - it has an 'id' and so we can simply search for the
element with the XmlTag.findId
method on the root of the tree. eg:
<rb:script>
var tag = rb.xml.root.findId("myBlock")
</rb:script>
Remember at this stage these tags have never been executed! If you now
call 'var text=tag.bodytext()' the result will be an empty string. To
generate output you have to first execute the tag. Do this using the
tag.execute()
method. The normal behaviour of this method is to run the tag on which it is
invoked. An exception occurs where the tag is an 'rb:block' tag - because
the effect of that tags execute is to do nothing! In the case of an rb:block
tag all of the children of the tag are executed. This will effectively
populate the output areas of each of the children. Note that in the example
above the block contains active server-side behaviour in the form of <rb:eval...>
tags. The block can be used in a loop to dump the contents of an object's properties: <rb:script>
var object = new Object;
object.name = "Peter";
object.sname = "Wilson";
var prop, testObj;
var tag = rb.xml.root.findId("myBlock")
rb.page.write('<table border="1">');
for (var prop in object) {
// generate new output for this row.
tag.execute();
// Now output to the client (adds to my
output area).
rb.page.write(tag.bodytext());
}
rb.page.write('</table>');
</rb:script>
|
|
(loadtime : 9ms) |