Previous Next

XML Data Source Applet

Example 7

This example shows how the JavaScript interface to the XML Java Parser can be used to program the XML Object Model directly. In this case we traverse all the items and multiply the PRICE and QUANTITY fields to come up with a total dollar value.

The test script that this button invokes is as follows:

<script> function CalcTotal() { var root = xmldso.getDocument().root; var items = root.children.item("ITEM"); var num = items.length; var total = 0; var count = 0; for (i = 0; i < num; i++) { var item = items.item(i).children; var price = item.item("PRICE").text; var quantity = item.item("QUANTITY").text; var q = parseInt(quantity); count += q; total = total + parseFloat(price) * q; } var msg = "+" + (total * 100); // get rid of floating total = parseInt(msg) / 100; // point noise. msg = "There are " + count + " books"; msg = msg + " with a total value of $" + total + "."; return msg; } </script>

© 1997 Microsoft Corporation. All rights reserved. Legal Notices.